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>
85 #include "remoteconf.h"
86 #include "localconf.h"
87 #include "grabmyaddr.h"
90 #include "isakmp_var.h"
96 #include "ipsec_doi.h"
98 #include "crypto_openssl.h"
100 #include "isakmp_ident.h"
101 #include "isakmp_agg.h"
102 #include "isakmp_base.h"
103 #include "isakmp_quick.h"
104 #include "isakmp_inf.h"
105 #include "isakmp_newg.h"
106 #include "vpn_control.h"
107 #include "vpn_control_var.h"
109 #include "vendorid.h"
110 #include "isakmp_xauth.h"
111 #include "isakmp_unity.h"
112 #include "isakmp_cfg.h"
115 #include "isakmp_frag.h"
117 #include "strnames.h"
122 # include "nattraversal.h"
124 #include "ike_session.h"
125 # include <netinet/in.h>
126 # include <netinet/udp.h>
127 # include <netinet/in_systm.h>
128 # include <netinet/ip.h>
129 # define SOL_UDP IPPROTO_UDP
130 #include "ipsecSessionTracer.h"
131 #include "ipsecMessageTracer.h"
132 #include "power_mgmt.h"
134 static int nostate1
__P((struct ph1handle
*, vchar_t
*));
135 static int nostate2
__P((struct ph2handle
*, vchar_t
*));
137 extern caddr_t
val2str(const char *, size_t);
139 static int (*ph1exchange
[][2][PHASE1ST_MAX
])
140 __P((struct ph1handle
*, vchar_t
*)) = {
143 /* Identity Protection exchange */
145 { nostate1
, ident_i1send
, nostate1
, ident_i2recv
, ident_i2send
,
146 ident_i3recv
, ident_i3send
, ident_i4recv
, ident_i4send
, nostate1
, },
147 { nostate1
, ident_r1recv
, ident_r1send
, ident_r2recv
, ident_r2send
,
148 ident_r3recv
, ident_r3send
, nostate1
, nostate1
, nostate1
, },
150 /* Aggressive exchange */
152 { nostate1
, agg_i1send
, nostate1
, agg_i2recv
, agg_i2send
,
153 nostate1
, nostate1
, nostate1
, nostate1
, nostate1
, },
154 { nostate1
, agg_r1recv
, agg_r1send
, agg_r2recv
, agg_r2send
,
155 nostate1
, nostate1
, nostate1
, nostate1
, nostate1
, },
159 { nostate1
, base_i1send
, nostate1
, base_i2recv
, base_i2send
,
160 base_i3recv
, base_i3send
, nostate1
, nostate1
, nostate1
, },
161 { nostate1
, base_r1recv
, base_r1send
, base_r2recv
, base_r2send
,
162 nostate1
, nostate1
, nostate1
, nostate1
, nostate1
, },
166 static int (*ph2exchange
[][2][PHASE2ST_MAX
])
167 __P((struct ph2handle
*, vchar_t
*)) = {
170 /* Quick mode for IKE */
172 { nostate2
, nostate2
, quick_i1prep
, nostate2
, quick_i1send
,
173 quick_i2recv
, quick_i2send
, quick_i3recv
, nostate2
, nostate2
, },
174 { nostate2
, quick_r1recv
, quick_r1prep
, nostate2
, quick_r2send
,
175 quick_r3recv
, quick_r3prep
, quick_r3send
, nostate2
, nostate2
, }
179 static u_char r_ck0
[] = { 0,0,0,0,0,0,0,0 }; /* used to verify the r_ck. */
181 static int isakmp_main
__P((vchar_t
*, struct sockaddr_storage
*, struct sockaddr_storage
*));
182 static int ph1_main
__P((struct ph1handle
*, vchar_t
*));
183 static int quick_main
__P((struct ph2handle
*, vchar_t
*));
184 static int isakmp_ph1begin_r
__P((vchar_t
*,
185 struct sockaddr_storage
*, struct sockaddr_storage
*, u_int8_t
));
186 static int isakmp_ph2begin_i
__P((struct ph1handle
*, struct ph2handle
*));
187 static int isakmp_ph2begin_r
__P((struct ph1handle
*, vchar_t
*));
188 static int etypesw1
__P((int));
189 static int etypesw2
__P((int));
191 static int frag_handler(struct ph1handle
*,
192 vchar_t
*, struct sockaddr_storage
*, struct sockaddr_storage
*);
196 * isakmp packet handler
199 isakmp_handler(so_isakmp
)
202 struct isakmp isakmp
;
204 u_int64_t force_align
; // Wcast-align fix - force alignment
205 char buf
[sizeof (isakmp
) + 4];
206 u_int32_t non_esp
[2];
207 char lbuf
[sizeof(struct udphdr
) +
211 struct sockaddr_storage remote
;
212 struct sockaddr_storage local
;
213 unsigned int remote_len
= sizeof(remote
);
214 unsigned int local_len
= sizeof(local
);
218 vchar_t
*buf
= NULL
, *tmpbuf
= NULL
;
221 if (slept_at
|| woke_at
) {
222 plog(LLV_DEBUG
, LOCATION
, NULL
,
223 "ignoring isakmp port until power-mgmt event is handled.\n");
227 /* read message by MSG_PEEK */
228 while ((len
= recvfromto(so_isakmp
, x
.buf
, sizeof(x
),
229 MSG_PEEK
, &remote
, &remote_len
, &local
, &local_len
)) < 0) {
232 plog(LLV_ERROR
, LOCATION
, NULL
,
233 "failed to receive isakmp packet: %s\n",
238 /* keep-alive packet - ignore */
239 if (len
== 1 && (x
.buf
[0]&0xff) == 0xff) {
240 /* Pull the keep-alive packet */
241 if ((len
= recvfrom(so_isakmp
, (char *)x
.buf
, 1,
242 0, (struct sockaddr
*)&remote
, &remote_len
)) != 1) {
243 plog(LLV_ERROR
, LOCATION
, NULL
,
244 "failed to receive keep alive packet: %s\n",
250 /* Lucent IKE in UDP encapsulation */
255 udp
= ALIGNED_CAST(struct udphdr
*)&x
.lbuf
[0];
256 if (ntohs(udp
->uh_dport
) == 501) {
257 ip
= ALIGNED_CAST(struct ip
*)(x
.lbuf
+ sizeof(*udp
));
258 extralen
+= sizeof(*udp
) + ip
->ip_hl
;
263 /* we don't know about portchange yet,
264 look for non-esp marker instead */
265 if (x
.non_esp
[0] == 0 && x
.non_esp
[1] != 0)
266 extralen
= NON_ESP_MARKER_LEN
;
269 /* now we know if there is an extra non-esp
270 marker at the beginning or not */
271 memcpy ((char *)&isakmp
, x
.buf
+ extralen
, sizeof (isakmp
));
273 /* check isakmp header length, as well as sanity of header length */
274 if (len
< sizeof(isakmp
) || ntohl(isakmp
.len
) < sizeof(isakmp
)) {
275 plog(LLV_ERROR
, LOCATION
, &remote
,
276 "packet shorter than isakmp header size (%u, %u, %zu)\n",
277 len
, ntohl(isakmp
.len
), sizeof(isakmp
));
279 if ((len
= recvfrom(so_isakmp
, (char *)&isakmp
, sizeof(isakmp
),
280 0, (struct sockaddr
*)&remote
, &remote_len
)) < 0) {
281 plog(LLV_ERROR
, LOCATION
, NULL
,
282 "failed to receive isakmp packet: %s\n",
288 /* reject it if the size is tooooo big. */
289 if (ntohl(isakmp
.len
) > 0xffff) {
290 plog(LLV_ERROR
, LOCATION
, NULL
,
291 "the length in the isakmp header is too big.\n");
292 if ((len
= recvfrom(so_isakmp
, (char *)&isakmp
, sizeof(isakmp
),
293 0, (struct sockaddr
*)&remote
, &remote_len
)) < 0) {
294 plog(LLV_ERROR
, LOCATION
, NULL
,
295 "failed to receive isakmp packet: %s\n",
301 /* read real message */
302 if ((tmpbuf
= vmalloc(ntohl(isakmp
.len
) + extralen
)) == NULL
) {
303 plog(LLV_ERROR
, LOCATION
, NULL
,
304 "failed to allocate reading buffer (%u Bytes)\n",
305 ntohl(isakmp
.len
) + extralen
);
307 if ((len
= recvfrom(so_isakmp
, (char *)&isakmp
, sizeof(isakmp
),
308 0, (struct sockaddr
*)&remote
, &remote_len
)) < 0) {
309 plog(LLV_ERROR
, LOCATION
, NULL
,
310 "failed to receive isakmp packet: %s\n",
312 error
= -2; /* serious problem with socket */
317 while ((len
= recvfromto(so_isakmp
, (char *)tmpbuf
->v
, tmpbuf
->l
,
318 0, &remote
, &remote_len
, &local
, &local_len
)) < 0) {
321 plog(LLV_ERROR
, LOCATION
, NULL
,
322 "failed to receive isakmp packet: %s\n",
327 if (len
< extralen
) {
328 plog(LLV_ERROR
, LOCATION
, NULL
,
329 "invalid len (%d Bytes) & extralen (%d Bytes)\n",
334 if ((buf
= vmalloc(len
- extralen
)) == NULL
) {
335 plog(LLV_ERROR
, LOCATION
, NULL
,
336 "failed to allocate reading buffer (%u Bytes)\n",
341 memcpy (buf
->v
, tmpbuf
->v
+ extralen
, buf
->l
);
346 plog(LLV_ERROR
, LOCATION
, &remote
, "received invalid length (%d != %zu), why ?\n",
351 plog(LLV_DEBUG
, LOCATION
, NULL
, "===\n");
352 plog(LLV_DEBUG
, LOCATION
, NULL
,
353 "%d bytes message received %s\n",
354 len
, saddr2str_fromto("from %s to %s",
355 (struct sockaddr
*)&remote
,
356 (struct sockaddr
*)&local
));
357 plogdump(LLV_DEBUG
, buf
->v
, buf
->l
);
359 /* avoid packets with malicious port/address */
360 switch (remote
.ss_family
) {
362 port
= ((struct sockaddr_in
*)&remote
)->sin_port
;
366 port
= ((struct sockaddr_in6
*)&remote
)->sin6_port
;
370 plog(LLV_ERROR
, LOCATION
, NULL
,
371 "invalid family: %d\n", remote
.ss_family
);
375 plog(LLV_ERROR
, LOCATION
, &remote
,
376 "src port == 0 (valid as UDP but not with IKE)\n");
380 /* XXX: check sender whether to be allowed or not to accept */
382 /* XXX: I don't know how to check isakmp half connection attack. */
384 /* simply reply if the packet was processed. */
385 if (check_recvdpkt(&remote
, &local
, buf
)) {
386 IPSECLOGASLMSG("Received retransmitted packet from %s.\n",
387 saddr2str((struct sockaddr
*)&remote
));
389 plog(LLV_NOTIFY
, LOCATION
, NULL
,
390 "the packet is retransmitted by %s.\n",
391 saddr2str((struct sockaddr
*)&remote
));
396 /* isakmp main routine */
397 if (isakmp_main(buf
, &remote
, &local
) != 0) goto end
;
411 * main processing to handle isakmp payload
414 isakmp_main(msg
, remote
, local
)
416 struct sockaddr_storage
*remote
, *local
;
418 struct isakmp
*isakmp
= (struct isakmp
*)msg
->v
;
419 isakmp_index
*index
= (isakmp_index
*)isakmp
;
420 u_int32_t msgid
= isakmp
->msgid
;
421 struct ph1handle
*iph1
;
423 #ifdef HAVE_PRINT_ISAKMP_C
424 isakmp_printpacket(msg
, remote
, local
, 0);
427 /* the initiator's cookie must not be zero */
428 if (memcmp(&isakmp
->i_ck
, r_ck0
, sizeof(cookie_t
)) == 0) {
429 plog(LLV_ERROR
, LOCATION
, remote
,
430 "malformed cookie received.\n");
434 /* Check the Major and Minor Version fields. */
436 * XXX Is is right to check version here ?
437 * I think it may no be here because the version depends
438 * on exchange status.
440 if (isakmp
->v
< ISAKMP_VERSION_NUMBER
) {
441 if (ISAKMP_GETMAJORV(isakmp
->v
) < ISAKMP_MAJOR_VERSION
) {
442 plog(LLV_ERROR
, LOCATION
, remote
,
443 "invalid major version %d.\n",
444 ISAKMP_GETMAJORV(isakmp
->v
));
447 #if ISAKMP_MINOR_VERSION > 0
448 if (ISAKMP_GETMINORV(isakmp
->v
) < ISAKMP_MINOR_VERSION
) {
449 plog(LLV_ERROR
, LOCATION
, remote
,
450 "invalid minor version %d.\n",
451 ISAKMP_GETMINORV(isakmp
->v
));
457 /* check the Flags field. */
458 /* XXX How is the exclusive check, E and A ? */
459 if (isakmp
->flags
& ~(ISAKMP_FLAG_E
| ISAKMP_FLAG_C
| ISAKMP_FLAG_A
)) {
460 plog(LLV_ERROR
, LOCATION
, remote
,
461 "invalid flag 0x%02x.\n", isakmp
->flags
);
465 /* ignore commit bit. */
466 if (ISSET(isakmp
->flags
, ISAKMP_FLAG_C
)) {
467 if (isakmp
->msgid
== 0) {
468 isakmp_info_send_nx(isakmp
, remote
, local
,
469 ISAKMP_NTYPE_INVALID_FLAGS
, NULL
);
470 plog(LLV_ERROR
, LOCATION
, remote
,
471 "Commit bit on phase1 forbidden.\n");
476 iph1
= getph1byindex(index
);
479 if (memcmp(&isakmp
->r_ck
, r_ck0
, sizeof(cookie_t
)) == 0 &&
480 iph1
->side
== INITIATOR
) {
481 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
482 IPSECSESSIONEVENTCODE_IKE_PACKET_RX_FAIL
,
483 CONSTSTR("malformed or unexpected cookie"),
484 CONSTSTR("Failed to process packet (malformed/unexpected cookie)"));
485 plog(LLV_DEBUG
, LOCATION
, remote
,
486 "malformed cookie received or "
487 "the initiator's cookies collide.\n");
492 /* Floating ports for NAT-T */
493 if (NATT_AVAILABLE(iph1
) &&
494 ! (iph1
->natt_flags
& NAT_PORTS_CHANGED
) &&
495 ((cmpsaddrstrict(iph1
->remote
, remote
) != 0) ||
496 (cmpsaddrstrict(iph1
->local
, local
) != 0)))
498 /* prevent memory leak */
499 racoon_free(iph1
->remote
);
500 racoon_free(iph1
->local
);
504 /* copy-in new addresses */
505 iph1
->remote
= dupsaddr((struct sockaddr
*)remote
);
506 if (iph1
->remote
== NULL
) {
507 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
508 IPSECSESSIONEVENTCODE_IKE_PACKET_RX_FAIL
,
509 CONSTSTR("failed to duplicate remote address"),
510 CONSTSTR("Failed to process phase1 message (can't duplicate remote address"));
511 plog(LLV_ERROR
, LOCATION
, iph1
->remote
,
512 "phase1 failed: dupsaddr failed.\n");
517 iph1
->local
= dupsaddr((struct sockaddr
*)local
);
518 if (iph1
->local
== NULL
) {
519 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
520 IPSECSESSIONEVENTCODE_IKE_PACKET_RX_FAIL
,
521 CONSTSTR("failed to duplicate local address"),
522 CONSTSTR("Failed to process phase1 message (can't duplicate local address"));
523 plog(LLV_ERROR
, LOCATION
, iph1
->remote
,
524 "phase1 failed: dupsaddr failed.\n");
530 /* set the flag to prevent further port floating
531 (FIXME: should we allow it? E.g. when the NAT gw
533 iph1
->natt_flags
|= NAT_PORTS_CHANGED
| NAT_ADD_NON_ESP_MARKER
;
535 /* print some neat info */
536 plog (LLV_INFO
, LOCATION
, NULL
,
537 "NAT-T: ports changed to: %s\n",
538 saddr2str_fromto("%s<->%s", (struct sockaddr
*)iph1
->remote
, (struct sockaddr
*)iph1
->local
));
541 /* must be same addresses in one stream of a phase at least. */
542 if (cmpsaddrstrict(iph1
->remote
, remote
) != 0) {
543 char *saddr_db
, *saddr_act
;
545 saddr_db
= racoon_strdup(saddr2str((struct sockaddr
*)iph1
->remote
));
546 saddr_act
= racoon_strdup(saddr2str((struct sockaddr
*)remote
));
547 STRDUP_FATAL(saddr_db
);
548 STRDUP_FATAL(saddr_act
);
550 plog(LLV_WARNING
, LOCATION
, remote
,
551 "remote address mismatched. db=%s, act=%s\n",
552 saddr_db
, saddr_act
);
554 racoon_free(saddr_db
);
555 racoon_free(saddr_act
);
559 * don't check of exchange type here because other type will be
560 * with same index, for example, informational exchange.
563 /* XXX more acceptable check */
566 // received ike packets: update dpd checks
567 isakmp_reschedule_info_monitor_if_pending(iph1
,
568 "ike packets received from peer");
572 switch (isakmp
->etype
) {
573 case ISAKMP_ETYPE_IDENT
:
574 case ISAKMP_ETYPE_AGG
:
575 case ISAKMP_ETYPE_BASE
:
576 /* phase 1 validity check */
577 if (isakmp
->msgid
!= 0) {
578 plog(LLV_ERROR
, LOCATION
, remote
,
579 "message id should be zero in phase1.\n");
583 /* search for isakmp status record of phase 1 */
586 * the packet must be the 1st message from a initiator
587 * or the 2nd message from the responder.
590 /* search for phase1 handle by index without r_ck */
591 iph1
= getph1byindex0(index
);
593 /*it must be the 1st message from a initiator.*/
594 if (memcmp(&isakmp
->r_ck
, r_ck0
,
595 sizeof(cookie_t
)) != 0) {
597 plog(LLV_DEBUG
, LOCATION
, remote
,
598 "malformed cookie received "
599 "or the spi expired.\n");
603 /* it must be responder's 1st exchange. */
604 if (isakmp_ph1begin_r(msg
, remote
, local
,
612 /* it must be the 2nd message from the responder. */
613 if (iph1
->side
!= INITIATOR
) {
614 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
615 IPSECSESSIONEVENTCODE_IKE_PACKET_RX_FAIL
,
616 CONSTSTR("malformed cookie and unexpected side"),
617 CONSTSTR("Failed to process phase1 message (unexpected side)"));
618 plog(LLV_DEBUG
, LOCATION
, remote
,
619 "malformed cookie received. "
620 "it has to be as the initiator. %s\n",
621 isakmp_pindex(&iph1
->index
, 0));
627 * Don't delete phase 1 handler when the exchange type
628 * in handler is not equal to packet's one because of no
629 * authencication completed.
631 if (iph1
->etype
!= isakmp
->etype
) {
632 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
633 IPSECSESSIONEVENTCODE_IKE_PACKET_RX_FAIL
,
634 CONSTSTR("mismatched exchange type"),
635 CONSTSTR("Failed to process phase1 message (mismatched exchange type)"));
636 plog(LLV_ERROR
, LOCATION
, iph1
->remote
,
637 "exchange type is mismatched: "
638 "db=%s packet=%s, ignore it.\n",
639 s_isakmp_etype(iph1
->etype
),
640 s_isakmp_etype(isakmp
->etype
));
645 if (isakmp
->np
== ISAKMP_NPTYPE_FRAG
)
646 return frag_handler(iph1
, msg
, remote
, local
);
649 /* call main process of phase 1 */
650 if (ph1_main(iph1
, msg
) < 0) {
651 plog(LLV_ERROR
, LOCATION
, iph1
->remote
,
652 "phase1 negotiation failed.\n");
659 case ISAKMP_ETYPE_AUTH
:
660 plog(LLV_INFO
, LOCATION
, remote
,
661 "unsupported exchange %d received.\n",
665 case ISAKMP_ETYPE_INFO
:
666 case ISAKMP_ETYPE_ACKINFO
:
668 * iph1 must be present for Information message.
669 * if iph1 is null then trying to get the phase1 status
670 * as the packet from responder againt initiator's 1st
671 * exchange in phase 1.
672 * NOTE: We think such informational exchange should be ignored.
675 iph1
= getph1byindex0(index
);
677 plog(LLV_ERROR
, LOCATION
, remote
,
678 "unknown Informational "
679 "exchange received.\n");
682 if (cmpsaddrstrict(iph1
->remote
, remote
) != 0) {
683 plog(LLV_WARNING
, LOCATION
, remote
,
684 "remote address mismatched. "
686 saddr2str((struct sockaddr
*)iph1
->remote
));
691 if (isakmp
->np
== ISAKMP_NPTYPE_FRAG
)
692 return frag_handler(iph1
, msg
, remote
, local
);
695 if (isakmp_info_recv(iph1
, msg
) < 0)
699 case ISAKMP_ETYPE_QUICK
:
701 struct ph2handle
*iph2
;
704 isakmp_info_send_nx(isakmp
, remote
, local
,
705 ISAKMP_NTYPE_INVALID_COOKIE
, NULL
);
706 plog(LLV_ERROR
, LOCATION
, remote
,
707 "can't start the quick mode, "
708 "there is no ISAKMP-SA, %s\n",
709 isakmp_pindex((isakmp_index
*)&isakmp
->i_ck
,
714 /* Reinit the IVM if it's still there */
715 if (iph1
->mode_cfg
&& iph1
->mode_cfg
->ivm
) {
716 oakley_delivm(iph1
->mode_cfg
->ivm
);
717 iph1
->mode_cfg
->ivm
= NULL
;
721 if (isakmp
->np
== ISAKMP_NPTYPE_FRAG
)
722 return frag_handler(iph1
, msg
, remote
, local
);
725 /* check status of phase 1 whether negotiated or not. */
726 if (iph1
->status
!= PHASE1ST_ESTABLISHED
) {
727 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
728 IPSECSESSIONEVENTCODE_IKEV1_PH2_INIT_DROP
,
729 CONSTSTR("can't start phase2 without valid phase1"),
730 CONSTSTR("Failed to start phase2 resonder (no established phase1"));
731 plog(LLV_ERROR
, LOCATION
, remote
,
732 "can't start the quick mode, "
733 "there is no valid ISAKMP-SA, %s\n",
734 isakmp_pindex(&iph1
->index
, iph1
->msgid
));
738 /* search isakmp phase 2 stauts record. */
739 iph2
= getph2bymsgid(iph1
, msgid
);
741 /* it must be new negotiation as responder */
742 if (isakmp_ph2begin_r(iph1
, msg
) < 0)
750 * we keep to set commit bit during negotiation.
751 * When SA is configured, bit will be reset.
753 * don't initiate commit bit. should be fixed in the future.
755 if (ISSET(isakmp
->flags
, ISAKMP_FLAG_C
))
756 iph2
->flags
|= ISAKMP_FLAG_C
;
758 if (ISSET(isakmp
->flags
, ISAKMP_FLAG_E
) &&
759 (iph2
->ph1
== NULL
|| iph2
->ph1
->approval
== NULL
)) {
760 IPSECSESSIONTRACEREVENT(iph2
->parent_session
,
761 IPSECSESSIONEVENTCODE_IKEV1_PH2_INIT_DROP
,
762 CONSTSTR("can't continue phase2 without valid phase1"),
763 CONSTSTR("Failed to continue phase2 resonder (invalid linked phase1"));
764 plog(LLV_ERROR
, LOCATION
, remote
,
765 "can't start the quick mode, "
766 "invalid linked ISAKMP-SA\n");
770 /* call main process of quick mode */
771 if (quick_main(iph2
, msg
) < 0) {
772 plog(LLV_ERROR
, LOCATION
, iph1
->remote
,
773 "phase2 negotiation failed.\n");
782 case ISAKMP_ETYPE_NEWGRP
:
784 plog(LLV_ERROR
, LOCATION
, remote
,
785 "Unknown new group mode exchange, "
786 "there is no ISAKMP-SA.\n");
791 if (isakmp
->np
== ISAKMP_NPTYPE_FRAG
)
792 return frag_handler(iph1
, msg
, remote
, local
);
795 isakmp_newgroup_r(iph1
, msg
);
799 case ISAKMP_ETYPE_CFG
:
801 plog(LLV_ERROR
, LOCATION
, NULL
,
802 "mode config %d from %s, "
803 "but we have no ISAKMP-SA.\n",
804 isakmp
->etype
, saddr2str((struct sockaddr
*)remote
));
807 if (iph1
->status
!= PHASE1ST_ESTABLISHED
) {
808 plog(LLV_ERROR
, LOCATION
, NULL
,
809 "mode config %d from %s, "
810 "but ISAKMP-SA %s isn't established.\n",
811 isakmp
->etype
, saddr2str((struct sockaddr
*)remote
),
812 isakmp_pindex(&iph1
->index
, iph1
->msgid
));
816 if (isakmp
->np
== ISAKMP_NPTYPE_FRAG
)
817 return frag_handler(iph1
, msg
, remote
, local
);
820 isakmp_cfg_r(iph1
, msg
);
824 case ISAKMP_ETYPE_NONE
:
826 plog(LLV_ERROR
, LOCATION
, NULL
,
827 "Invalid exchange type %d from %s.\n",
828 isakmp
->etype
, saddr2str((struct sockaddr
*)remote
));
836 * main function of phase 1.
840 struct ph1handle
*iph1
;
844 int ini_contact
= iph1
->rmconf
->ini_contact
;
846 struct timeval start
, end
;
849 u_int rekey_lifetime
;
851 /* ignore a packet */
852 if (iph1
->status
== PHASE1ST_ESTABLISHED
)
856 gettimeofday(&start
, NULL
);
859 if (ph1exchange
[etypesw1(iph1
->etype
)]
861 [iph1
->status
] == NULL
) {
862 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
863 IPSECSESSIONEVENTCODE_IKE_PACKET_RX_FAIL
,
864 CONSTSTR("unavailable function"),
865 CONSTSTR("Failed to process phase1 message (no state function)"));
866 plog(LLV_ERROR
, LOCATION
, iph1
->remote
,
867 "why isn't the function defined.\n");
870 error
= (ph1exchange
[etypesw1(iph1
->etype
)]
872 [iph1
->status
])(iph1
, msg
);
876 * When an invalid packet is received on phase1, it should
877 * be selected to process this packet. That is to respond
878 * with a notify and delete phase 1 handler, OR not to respond
879 * and keep phase 1 handler. However, in PHASE1ST_START when
880 * acting as RESPONDER we must not keep phase 1 handler or else
881 * it will stay forever.
884 if (iph1
->side
== RESPONDER
&& iph1
->status
== PHASE1ST_START
) {
885 plog(LLV_ERROR
, LOCATION
, iph1
->remote
,
886 "failed to pre-process packet.\n");
889 /* ignore the error and keep phase 1 handler */
895 /* free resend buffer */
896 if (iph1
->sendbuf
== NULL
) {
897 plog(LLV_ERROR
, LOCATION
, NULL
,
898 "no buffer found as sendbuf\n");
903 VPTRINIT(iph1
->sendbuf
);
905 /* turn off schedule */
906 SCHED_KILL(iph1
->scr
);
909 plog(LLV_DEBUG
, LOCATION
, NULL
, "===\n");
910 if ((ph1exchange
[etypesw1(iph1
->etype
)]
912 [iph1
->status
])(iph1
, msg
) != 0) {
913 plog(LLV_ERROR
, LOCATION
, iph1
->remote
,
914 "failed to process packet.\n");
919 gettimeofday(&end
, NULL
);
920 syslog(LOG_NOTICE
, "%s(%s): %8.6f",
921 "phase1", s_isakmp_state(iph1
->etype
, iph1
->side
, iph1
->status
),
922 timedelta(&start
, &end
));
924 if (iph1
->status
== PHASE1ST_ESTABLISHED
) {
927 gettimeofday(&iph1
->end
, NULL
);
928 syslog(LOG_NOTICE
, "%s(%s): %8.6f",
929 "phase1", s_isakmp_etype(iph1
->etype
),
930 timedelta(&iph1
->start
, &iph1
->end
));
933 #ifdef ENABLE_VPNCONTROL_PORT
935 if (iph1
->side
== RESPONDER
&&
936 iph1
->local
->ss_family
== AF_INET
) {
938 struct redirect
*addr
;
940 LIST_FOREACH(addr
, &lcconf
->redirect_addresses
, chain
) {
941 if (((struct sockaddr_in
*)iph1
->local
)->sin_addr
.s_addr
== addr
->cluster_address
) {
942 vchar_t
*raddr
= vmalloc(sizeof(u_int32_t
));
945 plog(LLV_ERROR
, LOCATION
, iph1
->remote
,
946 "failed to send redirect message - memory error.\n");
948 memcpy(raddr
->v
, &addr
->redirect_address
, sizeof(u_int32_t
));
949 (void)isakmp_info_send_n1(iph1
, ISAKMP_NTYPE_LOAD_BALANCE
, raddr
);
950 plog(LLV_DEBUG
, LOCATION
, iph1
->remote
, "sent redirect notification - address = %x.\n", ntohl(addr
->redirect_address
));
953 (void)ike_session_update_ph1_ph2tree(iph1
);
954 isakmp_ph1expire(iph1
);
962 /* save created date. */
963 (void)time(&iph1
->created
);
965 /* add to the schedule to expire, and save back pointer. */
966 iph1
->sce
= sched_new(iph1
->approval
->lifetime
,
967 isakmp_ph1expire_stub
, iph1
);
969 if (iph1
->rmconf
->initiate_ph1rekey
) {
970 if (iph1
->side
== INITIATOR
) {
971 spi_cmp
= memcmp(&iph1
->index
.i_ck
, &iph1
->index
.r_ck
, sizeof(iph1
->index
.i_ck
));
975 spi_cmp
= memcmp(&iph1
->index
.r_ck
, &iph1
->index
.i_ck
, sizeof(iph1
->index
.r_ck
));
979 rekey_lifetime
= ike_session_get_rekey_lifetime((spi_cmp
> 0),
980 iph1
->approval
->lifetime
);
981 if (rekey_lifetime
) {
982 iph1
->sce_rekey
= sched_new(rekey_lifetime
,
983 isakmp_ph1rekeyexpire_stub
,
986 /* iph1->approval->lifetime is too small (e.g. 1) so why bother?
989 plog(LLV_ERROR
, LOCATION
, iph1
->remote
,
990 "failed to get rekey timer - lifetime is too small... probably.\n");
995 /* ignore xauth if it is a rekey */
996 if (!iph1
->is_rekey
&&
997 iph1
->mode_cfg
->flags
& ISAKMP_CFG_VENDORID_XAUTH
) {
998 switch(AUTHMETHOD(iph1
)) {
999 case OAKLEY_ATTR_AUTH_METHOD_HYBRID_RSA_R
:
1000 case OAKLEY_ATTR_AUTH_METHOD_HYBRID_DSS_R
:
1001 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_PSKEY_R
:
1002 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSASIG_R
:
1003 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_DSSSIG_R
:
1004 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSAENC_R
:
1005 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSAREV_R
:
1006 xauth_sendreq(iph1
);
1007 /* XXX Don't process INITIAL_CONTACT */
1016 /* Schedule the r_u_there.... */
1017 if(iph1
->dpd_support
&& iph1
->rmconf
->dpd_interval
)
1018 isakmp_sched_r_u(iph1
, 0);
1021 /* INITIAL-CONTACT processing */
1022 /* ignore initial-contact if it is a rekey */
1023 /* don't send anything if local test mode. */
1024 if (!iph1
->is_rekey
&& !f_local
&& ini_contact
&& !getcontacted(iph1
->remote
)) {
1025 /* send INITIAL-CONTACT */
1026 isakmp_info_send_n1(iph1
,
1027 ISAKMP_NTYPE_INITIAL_CONTACT
, NULL
);
1028 /* insert a node into contacted list. */
1029 if (inscontacted(iph1
->remote
) == -1) {
1030 plog(LLV_ERROR
, LOCATION
, iph1
->remote
,
1031 "failed to add contacted list.\n");
1036 log_ph1established(iph1
);
1037 plog(LLV_DEBUG
, LOCATION
, NULL
, "===\n");
1040 * SA up shell script hook: do it now for rekeys, otherwise only
1041 * if ISAKMP mode config wasn't requested. In the later
1042 * case it is done when we receive the configuration.
1044 if ((iph1
->status
== PHASE1ST_ESTABLISHED
) &&
1045 (iph1
->is_rekey
|| !iph1
->rmconf
->mode_cfg
)) {
1046 switch (AUTHMETHOD(iph1
)) {
1047 #ifdef ENABLE_HYBRID
1048 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_PSKEY_R
:
1049 case OAKLEY_ATTR_AUTH_METHOD_HYBRID_RSA_R
:
1050 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSASIG_R
:
1051 /* Unimplemeted... */
1052 case OAKLEY_ATTR_AUTH_METHOD_HYBRID_DSS_R
:
1053 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_DSSSIG_R
:
1054 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSAENC_R
:
1055 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSAREV_R
:
1059 script_hook(iph1
, SCRIPT_PHASE1_UP
);
1064 ike_session_cleanup_other_established_ph1s(iph1
->parent_session
, iph1
);
1066 #ifdef ENABLE_VPNCONTROL_PORT
1067 vpncontrol_notify_phase_change(0, FROM_LOCAL
, iph1
, NULL
);
1068 vpncontrol_notify_peer_resp_ph1(1, iph1
);
1077 * main function of quick mode.
1080 quick_main(iph2
, msg
)
1081 struct ph2handle
*iph2
;
1084 struct isakmp
*isakmp
= (struct isakmp
*)msg
->v
;
1087 struct timeval start
, end
;
1090 /* ignore a packet */
1091 if (iph2
->status
== PHASE2ST_ESTABLISHED
1092 || iph2
->status
== PHASE2ST_GETSPISENT
)
1096 gettimeofday(&start
, NULL
);
1100 if (ph2exchange
[etypesw2(isakmp
->etype
)]
1102 [iph2
->status
] == NULL
) {
1103 plog(LLV_ERROR
, LOCATION
, iph2
->ph1
->remote
,
1104 "why isn't the function defined.\n");
1107 error
= (ph2exchange
[etypesw2(isakmp
->etype
)]
1109 [iph2
->status
])(iph2
, msg
);
1111 plog(LLV_ERROR
, LOCATION
, iph2
->ph1
->remote
,
1112 "failed to pre-process packet.\n");
1113 if (error
== ISAKMP_INTERNAL_ERROR
)
1115 isakmp_info_send_n1(iph2
->ph1
, error
, NULL
);
1119 /* when using commit bit, status will be reached here. */
1120 //if (iph2->status == PHASE2ST_ADDSA) //%%% BUG FIX - wrong place
1123 /* free resend buffer */
1124 if (iph2
->sendbuf
== NULL
) {
1125 plog(LLV_ERROR
, LOCATION
, NULL
,
1126 "no buffer found as sendbuf\n");
1129 VPTRINIT(iph2
->sendbuf
);
1131 /* turn off schedule */
1132 SCHED_KILL(iph2
->scr
);
1134 /* when using commit bit, status will be reached here. */
1135 if (iph2
->status
== PHASE2ST_ADDSA
) //%%% BUG FIX - moved to here
1139 plog(LLV_DEBUG
, LOCATION
, NULL
, "===\n");
1140 if ((ph2exchange
[etypesw2(isakmp
->etype
)]
1142 [iph2
->status
])(iph2
, msg
) != 0) {
1143 plog(LLV_ERROR
, LOCATION
, iph2
->ph1
->remote
,
1144 "failed to process packet.\n");
1149 gettimeofday(&end
, NULL
);
1150 syslog(LOG_NOTICE
, "%s(%s): %8.6f",
1152 s_isakmp_state(ISAKMP_ETYPE_QUICK
, iph2
->side
, iph2
->status
),
1153 timedelta(&start
, &end
));
1159 /* new negotiation of phase 1 for initiator */
1161 isakmp_ph1begin_i(rmconf
, remote
, local
, started_by_api
)
1162 struct remoteconf
*rmconf
;
1163 struct sockaddr_storage
*remote
, *local
;
1166 struct ph1handle
*iph1
;
1168 struct timeval start
, end
;
1171 /* get new entry to isakmp status table. */
1176 iph1
->status
= PHASE1ST_START
;
1177 iph1
->rmconf
= rmconf
;
1178 if (link_rmconf_to_ph1(rmconf
) < 0) {
1179 plog(LLV_ERROR
, LOCATION
, remote
,
1181 "configuration.\n");
1182 iph1
->rmconf
= NULL
;
1183 /* don't call remph1(iph1) until after insph1(iph1) is called */
1187 iph1
->side
= INITIATOR
;
1188 iph1
->started_by_api
= started_by_api
;
1189 iph1
->version
= ISAKMP_VERSION_NUMBER
;
1194 iph1
->gssapi_state
= NULL
;
1196 #ifdef ENABLE_HYBRID
1197 if ((iph1
->mode_cfg
= isakmp_cfg_mkstate()) == NULL
) {
1198 /* don't call remph1(iph1) until after insph1(iph1) is called */
1205 if(rmconf
->ike_frag
== ISAKMP_FRAG_FORCE
)
1209 iph1
->frag_chain
= NULL
;
1211 iph1
->approval
= NULL
;
1213 /* XXX copy remote address */
1214 if (copy_ph1addresses(iph1
, rmconf
, remote
, local
) < 0) {
1215 /* don't call remph1(iph1) until after insph1(iph1) is called */
1216 iph1
= NULL
; /* deleted in copy_ph1addresses */
1222 if (ike_session_link_ph1_to_session(iph1
) != 0) {
1223 plog(LLV_DEBUG
, LOCATION
, NULL
, "Failed to link ph1 to session\n");
1228 // HACK!!! to track rekeys across SIGHUPs
1229 if (started_by_api
== VPN_RESTARTED_BY_API
&&
1231 iph1
->parent_session
->established
= 1;
1232 iph1
->parent_session
->ikev1_state
.ph2cnt
++;
1236 /* start phase 1 exchange */
1237 iph1
->etype
= rmconf
->etypes
->type
;
1239 plog(LLV_DEBUG
, LOCATION
, NULL
, "===\n");
1243 a
= racoon_strdup(saddr2str((struct sockaddr
*)iph1
->local
));
1246 plog(LLV_INFO
, LOCATION
, NULL
,
1247 "initiate new phase 1 negotiation: %s<=>%s\n",
1248 a
, saddr2str((struct sockaddr
*)iph1
->remote
));
1251 plog(LLV_INFO
, LOCATION
, NULL
,
1253 s_isakmp_etype(iph1
->etype
));
1256 gettimeofday(&iph1
->start
, NULL
);
1257 gettimeofday(&start
, NULL
);
1260 IPSECLOGASLMSG("IPSec Phase1 started (Initiated by me).\n");
1262 /* start exchange */
1263 if ((ph1exchange
[etypesw1(iph1
->etype
)]
1265 [iph1
->status
])(iph1
, NULL
) != 0) {
1266 /* failed to start phase 1 negotiation */
1274 gettimeofday(&end
, NULL
);
1275 syslog(LOG_NOTICE
, "%s(%s): %8.6f",
1277 s_isakmp_state(iph1
->etype
, iph1
->side
, iph1
->status
),
1278 timedelta(&start
, &end
));
1281 #ifdef ENABLE_VPNCONTROL_PORT
1282 vpncontrol_notify_phase_change(1, FROM_LOCAL
, iph1
, NULL
);
1288 /* new negotiation of phase 1 for responder */
1290 isakmp_ph1begin_r(msg
, remote
, local
, etype
)
1292 struct sockaddr_storage
*remote
, *local
;
1295 struct isakmp
*isakmp
= (struct isakmp
*)msg
->v
;
1296 struct remoteconf
*rmconf
;
1297 struct ph1handle
*iph1
;
1298 struct etypes
*etypeok
;
1300 struct timeval start
, end
;
1303 /* look for my configuration */
1304 rmconf
= getrmconf(remote
);
1305 if (rmconf
== NULL
) {
1306 plog(LLV_ERROR
, LOCATION
, remote
,
1308 "configuration.\n");
1312 /* check to be acceptable exchange type */
1313 etypeok
= check_etypeok(rmconf
, etype
);
1314 if (etypeok
== NULL
) {
1315 plog(LLV_ERROR
, LOCATION
, remote
,
1316 "not acceptable %s mode\n", s_isakmp_etype(etype
));
1320 /* get new entry to isakmp status table. */
1325 memcpy(&iph1
->index
.i_ck
, &isakmp
->i_ck
, sizeof(iph1
->index
.i_ck
));
1326 iph1
->status
= PHASE1ST_START
;
1327 iph1
->rmconf
= rmconf
;
1328 if (link_rmconf_to_ph1(rmconf
) < 0) {
1329 plog(LLV_ERROR
, LOCATION
, remote
,
1331 "configuration.\n");
1332 iph1
->rmconf
= NULL
;
1333 /* don't call remph1(iph1) until after insph1(iph1) is called */
1338 iph1
->side
= RESPONDER
;
1339 iph1
->started_by_api
= 0;
1340 iph1
->etype
= etypeok
->type
;
1341 iph1
->version
= isakmp
->v
;
1344 iph1
->gssapi_state
= NULL
;
1346 #ifdef ENABLE_HYBRID
1347 if ((iph1
->mode_cfg
= isakmp_cfg_mkstate()) == NULL
) {
1348 /* don't call remph1(iph1) until after insph1(iph1) is called */
1355 iph1
->frag_chain
= NULL
;
1357 iph1
->approval
= NULL
;
1360 /* RFC3947 says that we MUST accept new phases1 on NAT-T floated port.
1361 * We have to setup this flag now to correctly generate the first reply.
1362 * Don't know if a better check could be done for that ?
1364 if(extract_port(local
) == lcconf
->port_isakmp_natt
)
1365 iph1
->natt_flags
|= (NAT_PORTS_CHANGED
);
1368 /* copy remote address */
1369 if (copy_ph1addresses(iph1
, rmconf
, remote
, local
) < 0) {
1370 /* don't call remph1(iph1) until after insph1(iph1) is called */
1371 iph1
= NULL
; /* deleted in copy_ph1addresses */
1376 if (ike_session_link_ph1_to_session(iph1
) != 0) {
1382 plog(LLV_DEBUG
, LOCATION
, NULL
, "===\n");
1386 a
= racoon_strdup(saddr2str((struct sockaddr
*)iph1
->local
));
1389 plog(LLV_INFO
, LOCATION
, NULL
,
1390 "respond new phase 1 negotiation: %s<=>%s\n",
1391 a
, saddr2str((struct sockaddr
*)iph1
->remote
));
1394 plog(LLV_INFO
, LOCATION
, NULL
,
1395 "begin %s mode.\n", s_isakmp_etype(etype
));
1398 gettimeofday(&iph1
->start
, NULL
);
1399 gettimeofday(&start
, NULL
);
1402 IPSECLOGASLMSG("IPSec Phase1 started (Initiated by peer).\n");
1406 /* start exchange */
1407 if ((ph1exchange
[etypesw1(iph1
->etype
)]
1409 [iph1
->status
])(iph1
, msg
) < 0
1410 || (ph1exchange
[etypesw1(iph1
->etype
)]
1412 [iph1
->status
])(iph1
, msg
) < 0) {
1413 plog(LLV_ERROR
, LOCATION
, remote
,
1414 "failed to process packet.\n");
1421 gettimeofday(&end
, NULL
);
1422 syslog(LOG_NOTICE
, "%s(%s): %8.6f",
1424 s_isakmp_state(iph1
->etype
, iph1
->side
, iph1
->status
),
1425 timedelta(&start
, &end
));
1427 #ifdef ENABLE_VPNCONTROL_PORT
1428 vpncontrol_notify_phase_change(1, FROM_REMOTE
, iph1
, NULL
);
1433 #else /* ENABLE_FRAG */
1435 /* now that we have a phase1 handle, feed back into our
1436 * main receive function to catch fragmented packets
1439 return isakmp_main(msg
, remote
, local
);
1441 #endif /* ENABLE_FRAG */
1445 /* new negotiation of phase 2 for initiator */
1447 isakmp_ph2begin_i(iph1
, iph2
)
1448 struct ph1handle
*iph1
;
1449 struct ph2handle
*iph2
;
1451 #ifdef ENABLE_HYBRID
1452 if (xauth_check(iph1
) != 0) {
1453 plog(LLV_ERROR
, LOCATION
, NULL
,
1454 "Attempt to start phase 2 whereas Xauth failed\n");
1459 /* found ISAKMP-SA. */
1460 plog(LLV_DEBUG
, LOCATION
, NULL
, "===\n");
1461 plog(LLV_DEBUG
, LOCATION
, NULL
, "begin QUICK mode.\n");
1464 a
= racoon_strdup(saddr2str((struct sockaddr
*)iph2
->src
));
1467 plog(LLV_INFO
, LOCATION
, NULL
,
1468 "initiate new phase 2 negotiation: %s<=>%s\n",
1469 a
, saddr2str((struct sockaddr
*)iph2
->dst
));
1474 gettimeofday(&iph2
->start
, NULL
);
1476 /* found isakmp-sa */
1477 if (iph2
->ph1
&& iph1
!= iph2
->ph1
) {
1478 plog(LLV_DEBUG2
, LOCATION
, NULL
, "phase2 already bound in %s.\n",__FUNCTION__
);
1479 rebindph12(iph1
, iph2
);
1480 } else if (!iph2
->ph1
) {
1481 bindph12(iph1
, iph2
);
1484 if (ike_session_link_ph2_to_session(iph2
) != 0) {
1487 iph2
->status
= PHASE2ST_STATUS2
;
1489 IPSECLOGASLMSG("IPSec Phase2 started (Initiated by me).\n");
1491 if ((ph2exchange
[etypesw2(ISAKMP_ETYPE_QUICK
)]
1493 [iph2
->status
])(iph2
, NULL
) < 0) {
1497 #ifdef ENABLE_VPNCONTROL_PORT
1498 vpncontrol_notify_phase_change(1, FROM_LOCAL
, NULL
, iph2
);
1504 /* new negotiation of phase 2 for responder */
1506 isakmp_ph2begin_r(iph1
, msg
)
1507 struct ph1handle
*iph1
;
1510 struct isakmp
*isakmp
= (struct isakmp
*)msg
->v
;
1511 struct ph2handle
*iph2
= 0;
1514 struct timeval start
, end
;
1516 #ifdef ENABLE_HYBRID
1517 if (xauth_check(iph1
) != 0) {
1518 plog(LLV_ERROR
, LOCATION
, NULL
,
1519 "Attempt to start phase 2 whereas Xauth failed\n");
1526 plog(LLV_ERROR
, LOCATION
, NULL
,
1527 "failed to allocate phase2 entry.\n");
1532 iph2
->side
= RESPONDER
;
1533 iph2
->status
= PHASE2ST_START
;
1534 iph2
->flags
= isakmp
->flags
;
1535 iph2
->msgid
= isakmp
->msgid
;
1536 iph2
->seq
= pk_getseq();
1537 iph2
->ivm
= oakley_newiv2(iph1
, iph2
->msgid
);
1538 if (iph2
->ivm
== NULL
) {
1542 iph2
->dst
= dupsaddr((struct sockaddr
*)iph1
->remote
); /* XXX should be considered */
1543 if (iph2
->dst
== NULL
) {
1547 switch (iph2
->dst
->ss_family
) {
1550 ((struct sockaddr_in
*)iph2
->dst
)->sin_port
= 0;
1556 ((struct sockaddr_in6
*)iph2
->dst
)->sin6_port
= 0;
1561 plog(LLV_ERROR
, LOCATION
, NULL
,
1562 "invalid family: %d\n", iph2
->dst
->ss_family
);
1567 iph2
->src
= dupsaddr((struct sockaddr
*)iph1
->local
); /* XXX should be considered */
1568 if (iph2
->src
== NULL
) {
1572 switch (iph2
->src
->ss_family
) {
1575 ((struct sockaddr_in
*)iph2
->src
)->sin_port
= 0;
1581 ((struct sockaddr_in6
*)iph2
->src
)->sin6_port
= 0;
1586 plog(LLV_ERROR
, LOCATION
, NULL
,
1587 "invalid family: %d\n", iph2
->src
->ss_family
);
1592 /* add new entry to isakmp status table */
1594 bindph12(iph1
, iph2
);
1596 if (ike_session_link_ph2_to_session(iph2
) != 0) {
1603 plog(LLV_DEBUG
, LOCATION
, NULL
, "===\n");
1607 a
= racoon_strdup(saddr2str((struct sockaddr
*)iph2
->src
));
1610 plog(LLV_INFO
, LOCATION
, NULL
,
1611 "respond new phase 2 negotiation: %s<=>%s\n",
1612 a
, saddr2str((struct sockaddr
*)iph2
->dst
));
1617 gettimeofday(&start
, NULL
);
1620 IPSECLOGASLMSG("IPSec Phase2 started (Initiated by peer).\n");
1622 error
= (ph2exchange
[etypesw2(ISAKMP_ETYPE_QUICK
)]
1624 [iph2
->status
])(iph2
, msg
);
1626 plog(LLV_ERROR
, LOCATION
, iph1
->remote
,
1627 "failed to pre-process packet.\n");
1628 if (error
!= ISAKMP_INTERNAL_ERROR
)
1629 isakmp_info_send_n1(iph2
->ph1
, error
, NULL
);
1631 * release handler because it's wrong that ph2handle is kept
1632 * after failed to check message for responder's.
1641 plog(LLV_DEBUG
, LOCATION
, NULL
, "===\n");
1642 if ((ph2exchange
[etypesw2(isakmp
->etype
)]
1644 [iph2
->status
])(iph2
, msg
) < 0) {
1645 plog(LLV_ERROR
, LOCATION
, iph2
->ph1
->remote
,
1646 "failed to process packet.\n");
1647 /* don't release handler */
1651 gettimeofday(&end
, NULL
);
1652 syslog(LOG_NOTICE
, "%s(%s): %8.6f",
1654 s_isakmp_state(ISAKMP_ETYPE_QUICK
, iph2
->side
, iph2
->status
),
1655 timedelta(&start
, &end
));
1658 #ifdef ENABLE_VPNCONTROL_PORT
1659 vpncontrol_notify_phase_change(1, FROM_REMOTE
, NULL
, iph2
);
1667 * parse ISAKMP payloads, without ISAKMP base header.
1670 isakmp_parsewoh(np0
, gen
, len
)
1672 struct isakmp_gen
*gen
;
1675 u_char np
= np0
& 0xff;
1678 struct isakmp_parse_t
*p
, *ep
;
1680 plog(LLV_DEBUG
, LOCATION
, NULL
, "begin.\n");
1683 * 5 is a magic number, but any value larger than 2 should be fine
1684 * as we do vrealloc() in the following loop.
1686 result
= vmalloc(sizeof(struct isakmp_parse_t
) * 5);
1687 if (result
== NULL
) {
1688 plog(LLV_ERROR
, LOCATION
, NULL
,
1689 "failed to get buffer.\n");
1692 // Wcast-align fix (void*) - result = aligned buffer of struct isakmp_parse_t
1693 p
= ALIGNED_CAST(struct isakmp_parse_t
*)result
->v
;
1694 ep
= ALIGNED_CAST(struct isakmp_parse_t
*)(result
->v
+ result
->l
- sizeof(*ep
));
1698 /* parse through general headers */
1699 while (0 < tlen
&& np
!= ISAKMP_NPTYPE_NONE
) {
1700 if (tlen
<= sizeof(struct isakmp_gen
)) {
1701 /* don't send information, see isakmp_ident_r1() */
1702 plog(LLV_ERROR
, LOCATION
, NULL
,
1703 "invalid length of payload\n");
1708 plog(LLV_DEBUG
, LOCATION
, NULL
,
1709 "seen nptype=%u(%s)\n", np
, s_isakmp_nptype(np
));
1712 p
->len
= ntohs(gen
->len
);
1713 if (p
->len
< sizeof(struct isakmp_gen
) || p
->len
> tlen
) {
1714 plog(LLV_DEBUG
, LOCATION
, NULL
,
1715 "invalid length of payload\n");
1724 off
= p
- ALIGNED_CAST(struct isakmp_parse_t
*)result
->v
;
1725 result
= vrealloc(result
, result
->l
* 2);
1726 if (result
== NULL
) {
1727 plog(LLV_DEBUG
, LOCATION
, NULL
,
1728 "failed to realloc buffer.\n");
1732 ep
= ALIGNED_CAST(struct isakmp_parse_t
*)
1733 (result
->v
+ result
->l
- sizeof(*ep
));
1734 p
= ALIGNED_CAST(struct isakmp_parse_t
*)result
->v
;
1739 plen
= ntohs(gen
->len
);
1740 gen
= (struct isakmp_gen
*)((caddr_t
)gen
+ plen
);
1743 p
->type
= ISAKMP_NPTYPE_NONE
;
1747 plog(LLV_DEBUG
, LOCATION
, NULL
, "succeed.\n");
1753 * parse ISAKMP payloads, including ISAKMP base header.
1759 struct isakmp
*isakmp
= (struct isakmp
*)buf
->v
;
1760 struct isakmp_gen
*gen
;
1766 gen
= (struct isakmp_gen
*)(buf
->v
+ sizeof(*isakmp
));
1767 tlen
= buf
->l
- sizeof(struct isakmp
);
1768 result
= isakmp_parsewoh(np
, gen
, tlen
);
1774 isakmp_init(int ignore_phX
, int *tentative_failures
)
1776 /* initialize a isakmp status table */
1784 if (isakmp_open(tentative_failures
) < 0)
1802 * make strings containing i_cookie + r_cookie + msgid
1805 isakmp_pindex(index
, msgid
)
1806 const isakmp_index
*index
;
1807 const u_int32_t msgid
;
1809 static char buf
[64];
1813 memset(buf
, 0, sizeof(buf
));
1816 p
= (const u_char
*)index
;
1817 for (j
= 0, i
= 0; i
< sizeof(isakmp_index
); i
++) {
1818 snprintf((char *)&buf
[j
], sizeof(buf
) - j
, "%02x", p
[i
]);
1830 snprintf((char *)&buf
[j
], sizeof(buf
) - j
, ":%08x", ntohs(msgid
));
1835 /* open ISAKMP sockets. */
1837 isakmp_open(int *tentative_failures
)
1840 int ifnum
= 0, encap_ifnum
= 0;
1846 if (tentative_failures
) {
1847 *tentative_failures
= FALSE
;
1850 for (p
= lcconf
->myaddrs
; p
; p
= p
->next
) {
1853 if (p
->sock
!= -1) {
1857 continue; // socket already open
1860 /* warn if wildcard address - should we forbid this? */
1861 switch (p
->addr
->ss_family
) {
1863 if (((struct sockaddr_in
*)p
->addr
)->sin_addr
.s_addr
== 0)
1864 plog(LLV_WARNING
, LOCATION
, NULL
,
1865 "listening to wildcard address,"
1866 "broadcast IKE packet may kill you\n");
1870 if (IN6_IS_ADDR_UNSPECIFIED(&((struct sockaddr_in6
*)p
->addr
)->sin6_addr
))
1871 plog(LLV_WARNING
, LOCATION
, NULL
,
1872 "listening to wildcard address, "
1873 "broadcast IKE packet may kill you\n");
1877 plog(LLV_ERROR
, LOCATION
, NULL
,
1878 "unsupported address family %d\n",
1879 lcconf
->default_af
);
1884 if (p
->addr
->ss_family
== AF_INET6
&&
1885 IN6_IS_ADDR_MULTICAST(&((struct sockaddr_in6
*)
1886 p
->addr
)->sin6_addr
))
1888 plog(LLV_DEBUG
, LOCATION
, NULL
,
1889 "Ignoring multicast address %s\n",
1890 saddr2str((struct sockaddr
*)p
->addr
));
1891 racoon_free(p
->addr
);
1897 if ((p
->sock
= socket(p
->addr
->ss_family
, SOCK_DGRAM
, 0)) < 0) {
1898 plog(LLV_ERROR
, LOCATION
, NULL
,
1899 "socket (%s)\n", strerror(errno
));
1903 if (fcntl(p
->sock
, F_SETFL
, O_NONBLOCK
) == -1)
1904 plog(LLV_ERROR
, LOCATION
, NULL
,
1905 "failed to put socket in non-blocking mode\n");
1907 /* receive my interface address on inbound packets. */
1908 switch (p
->addr
->ss_family
) {
1910 if (setsockopt(p
->sock
, IPPROTO_IP
,
1912 (const void *)&yes
, sizeof(yes
)) < 0) {
1913 plog(LLV_ERROR
, LOCATION
, NULL
,
1914 "setsockopt IP_RECVDSTADDR (%s)\n",
1922 pktinfo
= IPV6_RECVPKTINFO
;
1924 pktinfo
= IPV6_RECVDSTADDR
;
1926 if (setsockopt(p
->sock
, IPPROTO_IPV6
, pktinfo
,
1927 (const void *)&yes
, sizeof(yes
)) < 0)
1929 plog(LLV_ERROR
, LOCATION
, NULL
,
1930 "setsockopt IPV6_RECVDSTADDR (%d):%s\n",
1931 pktinfo
, strerror(errno
));
1938 #ifdef IPV6_USE_MIN_MTU
1939 if (p
->addr
->ss_family
== AF_INET6
&&
1940 setsockopt(p
->sock
, IPPROTO_IPV6
, IPV6_USE_MIN_MTU
,
1941 (void *)&yes
, sizeof(yes
)) < 0) {
1942 plog(LLV_ERROR
, LOCATION
, NULL
,
1943 "setsockopt IPV6_USE_MIN_MTU (%s)\n",
1949 if (setsockopt_bypass(p
->sock
, p
->addr
->ss_family
) < 0)
1952 if (extract_port(p
->addr
) == PORT_ISAKMP
) {
1953 if (setsockopt(p
->sock
, SOL_SOCKET
, SO_NOTIFYCONFLICT
,
1954 (void *)&yes
, sizeof(yes
)) < 0) {
1955 plog(LLV_ERROR
, LOCATION
, p
->addr
,
1956 "setsockopt (%s)\n", strerror(errno
));
1961 if (bind(p
->sock
, (struct sockaddr
*)p
->addr
, sysdep_sa_len((struct sockaddr
*)p
->addr
)) < 0) {
1962 int tmp_errno
= errno
;
1963 plog(LLV_ERROR
, LOCATION
, p
->addr
,
1964 "failed to bind to address %s (%s).\n",
1965 saddr2str((struct sockaddr
*)p
->addr
), strerror(tmp_errno
));
1967 // if bind failed b/c of a tentative v6 address, try again later
1968 if (tmp_errno
== EADDRNOTAVAIL
&& p
->addr
->ss_family
== AF_INET6
) {
1969 struct in6_ifreq ifr6
;
1971 bzero(&ifr6
, sizeof(ifr6
));
1972 strlcpy(ifr6
.ifr_name
, p
->ifname
, sizeof(ifr6
.ifr_name
));
1973 memcpy(&ifr6
.ifr_addr
, p
->addr
, sizeof(ifr6
.ifr_addr
));
1974 if (ioctl(p
->sock
, SIOCGIFAFLAG_IN6
, &ifr6
) >= 0) {
1976 * the tentative flag may have cleared between the bind() and ioctl() calls (i.e due to timing), so
1977 * try infering that it was tentative from ensuring the absense other cases of EADDRNOTAVAIL.
1979 if ((ifr6
.ifr_ifru
.ifru_flags6
& (IN6_IFF_ANYCAST
| IN6_IFF_DUPLICATED
| IN6_IFF_DETACHED
| IN6_IFF_DEPRECATED
)) == 0) {
1980 // address may have been tentantive... invalidate sock but leave address around for another try later
1981 plog(LLV_ERROR
, LOCATION
, p
->addr
,
1982 "failed to bind to address %s: because interface address is/was not ready (flags %x).\n",
1983 saddr2str((struct sockaddr
*)p
->addr
), ifr6
.ifr_ifru
.ifru_flags6
);
1986 if (tentative_failures
) {
1987 *tentative_failures
= TRUE
;
1991 plog(LLV_ERROR
, LOCATION
, p
->addr
,
1992 "failed to bind to address %s: because of interface address error, flags %x.\n",
1993 saddr2str((struct sockaddr
*)p
->addr
), ifr6
.ifr_ifru
.ifru_flags6
);
1996 plog(LLV_ERROR
, LOCATION
, p
->addr
,
1997 "failed to bind to address %s: can't read interface address flags.\n",
1998 saddr2str((struct sockaddr
*)p
->addr
));
2011 plog(LLV_INFO
, LOCATION
, NULL
,
2012 "%s used as isakmp port (fd=%d)\n",
2013 saddr2str((struct sockaddr
*)p
->addr
), p
->sock
);
2017 racoon_free(p
->addr
);
2019 if (! lcconf
->autograbaddr
&& lcconf
->strict_address
)
2025 plog(LLV_ERROR
, LOCATION
, NULL
,
2026 "no address could be bound.\n");
2031 if (natt_enabled_in_rmconf() && !encap_ifnum
) {
2032 plog(LLV_WARNING
, LOCATION
, NULL
,
2033 "NAT-T is enabled in at least one remote{} section,\n");
2034 plog(LLV_WARNING
, LOCATION
, NULL
,
2035 "but no 'isakmp_natt' address was specified!\n");
2045 isakmp_close_sockets();
2050 isakmp_close_sockets()
2054 for (p
= lcconf
->myaddrs
; p
; p
= p
->next
) {
2068 // close sockets for addresses that have gone away
2070 isakmp_close_unused()
2072 struct myaddrs
*p
, *next
, **prev
;
2074 prev
= &(lcconf
->myaddrs
);
2075 for (p
= lcconf
->myaddrs
; p
; p
= next
) {
2077 if (p
->in_use
== 0) { // not in use ?
2089 isakmp_send(iph1
, sbuf
)
2090 struct ph1handle
*iph1
;
2095 vchar_t
*vbuf
= NULL
;
2098 size_t extralen
= NON_ESP_MARKER_USE(iph1
) ? NON_ESP_MARKER_LEN
: 0;
2102 * Do not add the non ESP marker for a packet that will
2103 * be fragmented. The non ESP marker should appear in
2104 * all fragment's packets, but not in the fragmented packet
2106 if (iph1
->frag
&& sbuf
->l
> ISAKMP_FRAG_MAXLEN
)
2110 plog (LLV_DEBUG
, LOCATION
, NULL
, "Adding NON-ESP marker\n");
2112 /* If NAT-T port floating is in use, 4 zero bytes (non-ESP marker)
2113 must added just before the packet itself. For this we must
2114 allocate a new buffer and release it at the end. */
2116 if ((vbuf
= vmalloc (sbuf
->l
+ extralen
)) == NULL
) {
2117 plog(LLV_ERROR
, LOCATION
, NULL
,
2118 "vbuf allocation failed\n");
2121 *ALIGNED_CAST(u_int32_t
*)vbuf
->v
= 0;
2122 memcpy (vbuf
->v
+ extralen
, sbuf
->v
, sbuf
->l
);
2127 /* select the socket to be sent */
2128 s
= getsockmyaddr((struct sockaddr
*)iph1
->local
);
2135 plog (LLV_DEBUG
, LOCATION
, NULL
, "%zu bytes %s\n", sbuf
->l
,
2136 saddr2str_fromto("from %s to %s", (struct sockaddr
*)iph1
->local
, (struct sockaddr
*)iph1
->remote
));
2139 if (iph1
->frag
&& sbuf
->l
> ISAKMP_FRAG_MAXLEN
) {
2140 if (isakmp_sendfrags(iph1
, sbuf
) == -1) {
2141 plog(LLV_ERROR
, LOCATION
, NULL
,
2142 "isakmp_sendfrags failed\n");
2150 len
= sendfromto(s
, sbuf
->v
, sbuf
->l
,
2151 iph1
->local
, iph1
->remote
, lcconf
->count_persend
);
2153 plog(LLV_ERROR
, LOCATION
, NULL
, "sendfromto failed\n");
2166 /* called from scheduler */
2168 isakmp_ph1resend_stub(p
)
2171 struct ph1handle
*iph1
;
2173 iph1
=(struct ph1handle
*)p
;
2174 if(isakmp_ph1resend(iph1
) < 0){
2175 if(iph1
->scr
!= NULL
){
2176 /* Should not happen...
2178 sched_kill(iph1
->scr
);
2188 isakmp_ph1resend(iph1
)
2189 struct ph1handle
*iph1
;
2191 time_t retry_interval
;
2193 // make sure there is a buffer to send
2194 // isakmp_plist_set_all() could have returned NULL
2195 if (iph1
->sendbuf
== NULL
)
2198 /* Note: NEVER do the rem/del here, it will be done by the caller or by the _stub function
2200 if (iph1
->retry_counter
<= 0) {
2201 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
2202 IPSECSESSIONEVENTCODE_IKEV1_PH1_MAX_RETRANSMIT
,
2203 CONSTSTR("Phase1 Maximum Retransmits"),
2204 CONSTSTR("Phase1 negotiation failed (Maximum retransmits)"));
2206 plog(LLV_ERROR
, LOCATION
, NULL
,
2207 "phase1 negotiation failed due to time up. %s\n",
2208 isakmp_pindex(&iph1
->index
, iph1
->msgid
));
2209 EVT_PUSH(iph1
->local
, iph1
->remote
,
2210 EVTT_PEER_NO_RESPONSE
, NULL
);
2211 if (iph1
->side
== INITIATOR
&& iph1
->is_rekey
&& iph1
->parent_session
&& iph1
->parent_session
->is_client
) {
2212 /* to get around a bug on the peer, in which rekeys to port 4500 are dropped */
2213 if (isakmp_ph1rekeyretry(iph1
) == 0)
2218 ike_session_ph1_retransmits(iph1
);
2221 if (isakmp_send(iph1
, iph1
->sendbuf
) < 0){
2222 if (iph1
->rmconf
->retry_counter
!= iph1
->retry_counter
) {
2223 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
2224 IPSECSESSIONEVENTCODE_IKE_PACKET_TX_FAIL
,
2225 CONSTSTR("Phase1 Retransmit"),
2226 CONSTSTR("Failed to retrasmit Phase1"));
2228 plog(LLV_ERROR
, LOCATION
, iph1
->remote
,
2229 "phase1 negotiation failed due to send error. %s\n",
2230 isakmp_pindex(&iph1
->index
, iph1
->msgid
));
2231 EVT_PUSH(iph1
->local
, iph1
->remote
,
2232 EVTT_PEER_NO_RESPONSE
, NULL
);
2236 if (iph1
->rmconf
->retry_counter
!= iph1
->retry_counter
) {
2237 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
2238 IPSECSESSIONEVENTCODE_IKE_PACKET_TX_SUCC
,
2239 CONSTSTR("Phase1 Retransmit"),
2243 plog(LLV_DEBUG
, LOCATION
, iph1
->remote
,
2244 "resend phase1 packet %s\n",
2245 isakmp_pindex(&iph1
->index
, iph1
->msgid
));
2247 iph1
->retry_counter
--;
2248 retry_interval
= get_exp_retx_interval((iph1
->rmconf
->retry_counter
- iph1
->retry_counter
),
2249 iph1
->rmconf
->retry_interval
);
2250 iph1
->scr
= sched_new(retry_interval
,
2251 isakmp_ph1resend_stub
, iph1
);
2256 /* called from scheduler */
2258 isakmp_ph2resend_stub(p
)
2261 struct ph2handle
*iph2
;
2263 iph2
=(struct ph2handle
*)p
;
2265 if(isakmp_ph2resend(iph2
) < 0){
2273 isakmp_ph2resend(iph2
)
2274 struct ph2handle
*iph2
;
2276 time_t retry_interval
;
2278 /* Note: NEVER do the unbind/rem/del here, it will be done by the caller or by the _stub function
2280 //%%% BUG FIX - related to commit bit usage - crash happened here
2281 if (iph2
->ph1
== 0) {
2282 plog(LLV_ERROR
, LOCATION
, NULL
,
2283 "internal error - attempt to re-send phase2 with no phase1 bound.\n");
2287 if (iph2
->ph1
->status
== PHASE1ST_EXPIRED
){
2288 IPSECSESSIONTRACEREVENT(iph2
->ph1
->parent_session
,
2289 IPSECSESSIONEVENTCODE_IKEV1_PH2_MAX_RETRANSMIT
,
2290 CONSTSTR("Underlying Phase1 expired"),
2291 CONSTSTR("Failed to retransmit phase2 (underlying phase1 expired)"));
2292 plog(LLV_ERROR
, LOCATION
, iph2
->ph1
->remote
,
2293 "phase2 negotiation failed due to phase1 expired. %s\n",
2294 isakmp_pindex(&iph2
->ph1
->index
, iph2
->msgid
));
2298 if (iph2
->retry_counter
<= 0) {
2299 IPSECSESSIONTRACEREVENT(iph2
->ph1
->parent_session
,
2300 IPSECSESSIONEVENTCODE_IKEV1_PH2_MAX_RETRANSMIT
,
2301 CONSTSTR("Phase2 maximum retransmits"),
2302 CONSTSTR("Phase2 negotiation failed (maximum retransmits)"));
2303 plog(LLV_ERROR
, LOCATION
, iph2
->ph1
->remote
,
2304 "phase2 negotiation failed due to time up. %s\n",
2305 isakmp_pindex(&iph2
->ph1
->index
, iph2
->msgid
));
2306 EVT_PUSH(iph2
->src
, iph2
->dst
, EVTT_PEER_NO_RESPONSE
, NULL
);
2309 ike_session_ph2_retransmits(iph2
);
2312 if (isakmp_send(iph2
->ph1
, iph2
->sendbuf
) < 0){
2313 if (iph2
->ph1
->rmconf
->retry_counter
!= iph2
->retry_counter
) {
2314 IPSECSESSIONTRACEREVENT(iph2
->ph1
->parent_session
,
2315 IPSECSESSIONEVENTCODE_IKE_PACKET_TX_FAIL
,
2316 CONSTSTR("Phase2 Retransmit"),
2317 CONSTSTR("Failed to retransmit Phase2 message"));
2319 plog(LLV_ERROR
, LOCATION
, NULL
,
2320 "phase2 negotiation failed due to send error. %s\n",
2321 isakmp_pindex(&iph2
->ph1
->index
, iph2
->msgid
));
2322 EVT_PUSH(iph2
->src
, iph2
->dst
, EVTT_PEER_NO_RESPONSE
, NULL
);
2326 if (iph2
->ph1
->rmconf
->retry_counter
!= iph2
->retry_counter
) {
2327 IPSECSESSIONTRACEREVENT(iph2
->ph1
->parent_session
,
2328 IPSECSESSIONEVENTCODE_IKE_PACKET_TX_SUCC
,
2329 CONSTSTR("Phase2 Retransmit"),
2333 plog(LLV_DEBUG
, LOCATION
, NULL
,
2334 "resend phase2 packet %s\n",
2335 isakmp_pindex(&iph2
->ph1
->index
, iph2
->msgid
));
2337 iph2
->retry_counter
--;
2338 retry_interval
= get_exp_retx_interval((iph2
->ph1
->rmconf
->retry_counter
- iph2
->ph1
->retry_counter
),
2339 iph2
->ph1
->rmconf
->retry_interval
);
2340 iph2
->scr
= sched_new(retry_interval
,
2341 isakmp_ph2resend_stub
, iph2
);
2345 isakmp_reschedule_info_monitor_if_pending(iph2
->ph1
,
2346 "phase2 packets sent to peer: retransmit timer armed");
2353 /* called from scheduler */
2355 isakmp_ph1expire_stub(p
)
2359 isakmp_ph1expire((struct ph1handle
*)p
);
2363 isakmp_ph1expire(iph1
)
2364 struct ph1handle
*iph1
;
2368 SCHED_KILL(iph1
->sce
);
2370 SCHED_KILL(iph1
->dpd_r_u
);
2373 if(iph1
->status
!= PHASE1ST_EXPIRED
){
2374 src
= racoon_strdup(saddr2str((struct sockaddr
*)iph1
->local
));
2375 dst
= racoon_strdup(saddr2str((struct sockaddr
*)iph1
->remote
));
2379 plog(LLV_INFO
, LOCATION
, NULL
,
2380 "ISAKMP-SA expired %s-%s spi:%s\n",
2382 isakmp_pindex(&iph1
->index
, 0));
2385 iph1
->status
= PHASE1ST_EXPIRED
;
2386 (void)ike_session_update_ph1_ph2tree(iph1
);
2390 * the phase1 deletion is postponed until there is no phase2.
2392 if (LIST_FIRST(&iph1
->ph2tree
) != NULL
) {
2393 iph1
->sce
= sched_new(1, isakmp_ph1expire_stub
, iph1
);
2397 iph1
->sce
= sched_new(1, isakmp_ph1delete_stub
, iph1
);
2400 /* called from scheduler */
2402 isakmp_ph1rekeyexpire_stub(p
)
2406 isakmp_ph1rekeyexpire((struct ph1handle
*)p
, FALSE
);
2410 isakmp_ph1rekeyexpire(iph1
, ignore_sess_drop_policy
)
2411 struct ph1handle
*iph1
;
2412 int ignore_sess_drop_policy
;
2415 struct remoteconf
*rmconf
;
2417 SCHED_KILL(iph1
->sce_rekey
);
2419 // early exit if iph2->sce == NULL, iph2 isn't established or if entire session is going down
2420 if (iph1
->sce
== NULL
||
2421 iph1
->status
!= PHASE1ST_ESTABLISHED
||
2426 src
= racoon_strdup(saddr2str((struct sockaddr
*)iph1
->local
));
2427 dst
= racoon_strdup(saddr2str((struct sockaddr
*)iph1
->remote
));
2431 plog(LLV_INFO
, LOCATION
, NULL
,
2432 "ISAKMP-SA rekey-timer expired %s-%s spi:%s\n",
2434 isakmp_pindex(&iph1
->index
, 0));
2438 if (!ignore_sess_drop_policy
&& ike_session_drop_rekey(iph1
->parent_session
, IKE_SESSION_REKEY_TYPE_PH1
)) {
2442 // exit if there is another ph1 that is established (with a pending rekey timer)
2443 if (ike_session_has_other_established_ph1(iph1
->parent_session
, iph1
)) {
2444 plog(LLV_INFO
, LOCATION
, iph1
->remote
,
2445 "request for ISAKMP-SA rekey was ignored "
2446 "due to another established ph1.\n");
2450 // if there is another ph1 that is negotiating, postpone this rekey for a few seconds later
2451 if (ike_session_has_other_negoing_ph1(iph1
->parent_session
, iph1
)) {
2452 plog(LLV_DEBUG
, LOCATION
, NULL
, "reschedule Phase1 rekey.\n");
2453 iph1
->sce_rekey
= sched_new(1,
2454 isakmp_ph1rekeyexpire_stub
,
2459 // get rmconf to initiate rekey with
2460 rmconf
= iph1
->rmconf
;
2461 if (!rmconf
|| rmconf
->to_delete
|| rmconf
->to_remove
) {
2462 rmconf
= getrmconf(iph1
->remote
);
2465 /* begin quick mode */
2466 plog(LLV_DEBUG
, LOCATION
, NULL
, "begin Phase1 rekey.\n");
2468 /* start phase 1 negotiation as a initiator. */
2469 if (isakmp_ph1begin_i(rmconf
, iph1
->remote
, iph1
->local
, 0) < 0) {
2470 plog(LLV_DEBUG
, LOCATION
, NULL
, "Phase1 rekey Failed.\n");
2473 plog(LLV_ERROR
, LOCATION
, NULL
,
2474 "Phase1 rekey failed: no configuration found for %s.\n",
2475 saddrwop2str((struct sockaddr
*)iph1
->remote
));
2480 isakmp_ph1rekeyretry(iph1
)
2481 struct ph1handle
*iph1
;
2484 struct remoteconf
*rmconf
;
2486 // this code path is meant for floated ph1 rekeys that are failing on the first message
2487 if (iph1
->sce
!= NULL
||
2488 iph1
->sce_rekey
!= NULL
||
2489 (iph1
->status
!= PHASE1ST_MSG1SENT
|| ((iph1
->natt_flags
& NAT_PORTS_CHANGED
) == 0)) ||
2490 (extract_port(iph1
->local
) != PORT_ISAKMP_NATT
&& extract_port(iph1
->remote
) != PORT_ISAKMP_NATT
) ||
2495 src
= racoon_strdup(saddr2str((struct sockaddr
*)iph1
->local
));
2496 dst
= racoon_strdup(saddr2str((struct sockaddr
*)iph1
->remote
));
2500 plog(LLV_INFO
, LOCATION
, NULL
,
2501 "ISAKMP-SA rekey failed... retrying %s-%s spi:%s\n",
2503 isakmp_pindex(&iph1
->index
, 0));
2507 if (ike_session_drop_rekey(iph1
->parent_session
, IKE_SESSION_REKEY_TYPE_PH1
)) {
2508 plog(LLV_INFO
, LOCATION
, iph1
->remote
,
2509 "request for ISAKMP-SA rekey was ignored "
2510 "due to idleness.\n");
2514 // exit if there is another ph1 that is established (with a pending rekey timer)
2515 if (ike_session_has_other_established_ph1(iph1
->parent_session
, iph1
)) {
2516 plog(LLV_INFO
, LOCATION
, iph1
->remote
,
2517 "request to retry ISAKMP-SA rekey was ignored "
2518 "due to another established ph1.\n");
2522 // some servers don't like respond to 4500 for rekeys... try accomodate them
2523 if (extract_port(iph1
->local
) == PORT_ISAKMP_NATT
) {
2524 set_port(iph1
->local
, PORT_ISAKMP
);
2526 if (extract_port(iph1
->remote
) == PORT_ISAKMP_NATT
) {
2527 set_port(iph1
->remote
, PORT_ISAKMP
);
2529 iph1
->natt_flags
&= ~NAT_PORTS_CHANGED
;
2530 rmconf
= getrmconf(iph1
->remote
);
2532 /* begin quick mode */
2533 plog(LLV_DEBUG
, LOCATION
, NULL
, "begin Phase1 rekey retry.\n");
2535 /* start phase 1 negotiation as a initiator. */
2536 if (isakmp_ph1begin_i(rmconf
, iph1
->remote
, iph1
->local
, 0) < 0) {
2537 plog(LLV_DEBUG
, LOCATION
, NULL
, "Phase1 rekey retry Failed.\n");
2541 plog(LLV_ERROR
, LOCATION
, NULL
,
2542 "Phase1 rekey retry failed: no configuration found for %s.\n",
2543 saddrwop2str((struct sockaddr
*)iph1
->remote
));
2549 /* called from scheduler */
2551 isakmp_ph1delete_stub(p
)
2555 isakmp_ph1delete((struct ph1handle
*)p
);
2559 isakmp_ph1delete(iph1
)
2560 struct ph1handle
*iph1
;
2564 SCHED_KILL(iph1
->sce
);
2565 SCHED_KILL(iph1
->sce_rekey
);
2567 SCHED_KILL(iph1
->dpd_r_u
);
2570 if (LIST_FIRST(&iph1
->ph2tree
) != NULL
) {
2571 iph1
->sce
= sched_new(1, isakmp_ph1delete_stub
, iph1
);
2575 isakmp_info_send_d1(iph1
);
2577 /* don't re-negosiation when the phase 1 SA expires. */
2579 src
= racoon_strdup(saddr2str((struct sockaddr
*)iph1
->local
));
2580 dst
= racoon_strdup(saddr2str((struct sockaddr
*)iph1
->remote
));
2584 plog(LLV_INFO
, LOCATION
, NULL
,
2585 "ISAKMP-SA deleted %s-%s spi:%s\n",
2586 src
, dst
, isakmp_pindex(&iph1
->index
, 0));
2587 EVT_PUSH(iph1
->local
, iph1
->remote
, EVTT_PHASE1_DOWN
, NULL
);
2597 /* called from scheduler.
2598 * this function will call only isakmp_ph2delete().
2599 * phase 2 handler remain forever if kernel doesn't cry a expire of phase 2 SA
2600 * by something cause. That's why this function is called after phase 2 SA
2601 * expires in the userland.
2604 isakmp_ph2expire_stub(p
)
2608 isakmp_ph2expire((struct ph2handle
*)p
);
2612 isakmp_ph2expire(iph2
)
2613 struct ph2handle
*iph2
;
2617 if (iph2
->status
== PHASE2ST_EXPIRED
) {
2621 SCHED_KILL(iph2
->sce
);
2623 src
= racoon_strdup(saddrwop2str((struct sockaddr
*)iph2
->src
));
2624 dst
= racoon_strdup(saddrwop2str((struct sockaddr
*)iph2
->dst
));
2628 plog(LLV_INFO
, LOCATION
, NULL
,
2629 "phase2 sa expired %s-%s\n", src
, dst
);
2633 // delete outgoing SAs
2634 if (iph2
->status
== PHASE2ST_ESTABLISHED
&& iph2
->approval
) {
2637 for (pr
= iph2
->approval
->head
; pr
!= NULL
; pr
= pr
->next
) {
2639 pfkey_send_delete(lcconf
->sock_pfkey
,
2640 ipsecdoi2pfkey_proto(pr
->proto_id
),
2642 iph2
->src
, iph2
->dst
, pr
->spi_p
/* pr->reqid_out */);
2647 iph2
->status
= PHASE2ST_EXPIRED
;
2649 iph2
->sce
= sched_new(1, isakmp_ph2delete_stub
, iph2
);
2654 /* called from scheduler */
2656 isakmp_ph2delete_stub(p
)
2660 isakmp_ph2delete((struct ph2handle
*)p
);
2664 isakmp_ph2delete(iph2
)
2665 struct ph2handle
*iph2
;
2669 SCHED_KILL(iph2
->sce
);
2671 src
= racoon_strdup(saddrwop2str((struct sockaddr
*)iph2
->src
));
2672 dst
= racoon_strdup(saddrwop2str((struct sockaddr
*)iph2
->dst
));
2676 plog(LLV_INFO
, LOCATION
, NULL
,
2677 "phase2 sa deleted %s-%s\n", src
, dst
);
2689 * Interface between PF_KEYv2 and ISAKMP
2692 * receive ACQUIRE from kernel, and begin either phase1 or phase2.
2693 * if phase1 has been finished, begin phase2.
2696 isakmp_post_acquire(iph2
)
2697 struct ph2handle
*iph2
;
2699 struct remoteconf
*rmconf
;
2700 struct ph1handle
*iph1
= NULL
;
2702 plog(LLV_DEBUG
, LOCATION
, NULL
, "in post_acquire\n");
2704 /* search appropreate configuration with masking port. */
2705 rmconf
= getrmconf(iph2
->dst
);
2706 if (rmconf
== NULL
) {
2707 plog(LLV_ERROR
, LOCATION
, NULL
,
2708 "no configuration found for %s.\n",
2709 saddrwop2str((struct sockaddr
*)iph2
->dst
));
2713 /* if passive mode, ignore the acquire message */
2714 if (rmconf
->passive
) {
2715 plog(LLV_DEBUG
, LOCATION
, NULL
,
2716 "because of passive mode, "
2717 "ignore the acquire message for %s.\n",
2718 saddrwop2str((struct sockaddr
*)iph2
->dst
));
2722 if (ike_session_verify_ph2_parent_session(iph2
)) {
2723 plog(LLV_INFO
, LOCATION
, iph2
->dst
,
2724 "request for establishing IPsec-SA was ignored "
2725 "because there was a failure verifying parent session.\n");
2729 // what if there is another ph2 that is negotiating
2730 if (ike_session_has_other_negoing_ph2(iph2
->parent_session
, iph2
)) {
2731 // TODO: postpone this rekey for a second later
2732 plog(LLV_INFO
, LOCATION
, iph2
->dst
,
2733 "request for establishing IPsec-SA was ignored "
2734 "due to another negoing ph2.\n");
2738 // if this is a phase2 rekeys (the policy may not have the current port number).
2739 // so, use the appropriate ports.
2740 if (iph2
->is_rekey
) {
2741 ike_session_update_ph2_ports(iph2
);
2744 iph1
= ike_session_update_ph2_ph1bind(iph2
);
2746 /* no ISAKMP-SA found. */
2750 iph2
->retry_checkph1
= lcconf
->retry_checkph1
;
2751 sc
= sched_new(1, isakmp_chkph1there_stub
, iph2
);
2752 plog(LLV_INFO
, LOCATION
, NULL
,
2753 "IPsec-SA request for %s queued "
2754 "due to no phase1 found.\n",
2755 saddrwop2str((struct sockaddr
*)iph2
->dst
));
2757 // exit if there is another ph1 that is established (with a pending rekey timer)
2758 if (ike_session_has_negoing_ph1(iph2
->parent_session
)) {
2759 plog(LLV_INFO
, LOCATION
, iph2
->dst
,
2760 "request for phase1 was ignored "
2761 "due to another negotiating ph1.\n");
2765 /* start phase 1 negotiation as a initiator. */
2766 if (isakmp_ph1begin_i(rmconf
, iph2
->dst
, iph2
->src
, 0) < 0) {
2767 plog(LLV_INFO
, LOCATION
, iph2
->dst
,
2768 "request for phase1 failed. will try later.\n");
2775 /* found ISAKMP-SA, but on negotiation. */
2776 if (iph1
->status
!= PHASE1ST_ESTABLISHED
) {
2777 iph2
->retry_checkph1
= lcconf
->retry_checkph1
;
2778 sched_new(1, isakmp_chkph1there_stub
, iph2
);
2779 plog(LLV_INFO
, LOCATION
, iph2
->dst
,
2780 "request for establishing IPsec-SA was queued "
2781 "due to no phase1 found.\n");
2786 /* found established ISAKMP-SA */
2787 /* i.e. iph1->status == PHASE1ST_ESTABLISHED */
2789 /* found ISAKMP-SA. */
2790 plog(LLV_DEBUG
, LOCATION
, NULL
, "begin QUICK mode.\n");
2792 /* begin quick mode */
2793 if (isakmp_ph2begin_i(iph1
, iph2
))
2800 * receive GETSPI from kernel.
2803 isakmp_post_getspi(iph2
)
2804 struct ph2handle
*iph2
;
2807 struct timeval start
, end
;
2810 /* don't process it because there is no suitable phase1-sa. */
2811 if (iph2
->ph1
->status
== PHASE1ST_EXPIRED
) {
2812 plog(LLV_ERROR
, LOCATION
, iph2
->ph1
->remote
,
2813 "the negotiation is stopped, "
2814 "because there is no suitable ISAKMP-SA.\n");
2819 gettimeofday(&start
, NULL
);
2821 if ((ph2exchange
[etypesw2(ISAKMP_ETYPE_QUICK
)]
2823 [iph2
->status
])(iph2
, NULL
) != 0)
2826 gettimeofday(&end
, NULL
);
2827 syslog(LOG_NOTICE
, "%s(%s): %8.6f",
2829 s_isakmp_state(ISAKMP_ETYPE_QUICK
, iph2
->side
, iph2
->status
),
2830 timedelta(&start
, &end
));
2836 /* called by scheduler */
2838 isakmp_chkph1there_stub(p
)
2841 isakmp_chkph1there((struct ph2handle
*)p
);
2845 isakmp_chkph1there(iph2
)
2846 struct ph2handle
*iph2
;
2848 struct ph1handle
*iph1
;
2850 if (iph2
->status
!= PHASE2ST_STATUS2
||
2852 plog(LLV_DEBUG2
, LOCATION
, NULL
, "CHKPH1THERE: ph2 handle has advanced too far (status %d, STATUS2 %d, dying %d)... ignoring\n", iph2
->status
, PHASE2ST_STATUS2
, iph2
->is_dying
);
2856 iph2
->retry_checkph1
--;
2857 if (iph2
->retry_checkph1
< 0 ||
2858 ike_session_verify_ph2_parent_session(iph2
)) {
2859 if (iph2
->retry_checkph1
< 0) {
2860 plog(LLV_ERROR
, LOCATION
, iph2
->dst
,
2861 "phase2 negotiation failed "
2862 "due to time up waiting for phase1. %s\n",
2863 sadbsecas2str(iph2
->dst
, iph2
->src
,
2864 iph2
->satype
, 0, 0));
2866 plog(LLV_ERROR
, LOCATION
, iph2
->dst
,
2867 "phase2 negotiation failed "
2868 "due to invalid parent session. %s\n",
2869 sadbsecas2str(iph2
->dst
, iph2
->src
,
2870 iph2
->satype
, 0, 0));
2872 plog(LLV_INFO
, LOCATION
, NULL
,
2873 "delete phase 2 handler.\n");
2875 /* send acquire to kernel as error */
2876 pk_sendeacquire(iph2
);
2885 iph1
= ike_session_update_ph2_ph1bind(iph2
);
2887 /* XXX Even if ph1 as responder is there, should we not start
2888 * phase 2 negotiation ? */
2890 && iph1
->status
== PHASE1ST_ESTABLISHED
) {
2891 /* found isakmp-sa */
2893 plog(LLV_DEBUG2
, LOCATION
, NULL
, "CHKPH1THERE: got a ph1 handler, setting ports.\n");
2894 plog(LLV_DEBUG2
, LOCATION
, NULL
, "iph1->local: %s\n", saddr2str((struct sockaddr
*)iph1
->local
));
2895 plog(LLV_DEBUG2
, LOCATION
, NULL
, "iph1->remote: %s\n", saddr2str((struct sockaddr
*)iph1
->remote
));
2896 plog(LLV_DEBUG2
, LOCATION
, NULL
, "before:\n");
2897 plog(LLV_DEBUG2
, LOCATION
, NULL
, "src: %s\n", saddr2str((struct sockaddr
*)iph2
->src
));
2898 plog(LLV_DEBUG2
, LOCATION
, NULL
, "dst: %s\n", saddr2str((struct sockaddr
*)iph2
->dst
));
2899 set_port(iph2
->src
, extract_port(iph1
->local
));
2900 set_port(iph2
->dst
, extract_port(iph1
->remote
));
2901 plog(LLV_DEBUG2
, LOCATION
, NULL
, "After:\n");
2902 plog(LLV_DEBUG2
, LOCATION
, NULL
, "src: %s\n", saddr2str((struct sockaddr
*)iph2
->src
));
2903 plog(LLV_DEBUG2
, LOCATION
, NULL
, "dst: %s\n", saddr2str((struct sockaddr
*)iph2
->dst
));
2905 /* begin quick mode */
2906 if (isakmp_ph2begin_i(iph1
, iph2
)) {
2913 if (!ike_session_has_negoing_ph1(iph2
->parent_session
)) {
2914 struct remoteconf
*rmconf
= getrmconf(iph2
->dst
);
2915 /* start phase 1 negotiation as a initiator. */
2916 if (rmconf
== NULL
||
2917 isakmp_ph1begin_i(rmconf
, iph2
->dst
, iph2
->src
, 0) < 0) {
2918 plog(LLV_DEBUG2
, LOCATION
, NULL
, "CHKPH1THERE: no established/negoing ph1 handler found... failed to initiate new one\n");
2922 plog(LLV_DEBUG2
, LOCATION
, NULL
, "CHKPH1THERE: no established ph1 handler found\n");
2924 /* no isakmp-sa found */
2925 sched_new(1, isakmp_chkph1there_stub
, iph2
);
2930 /* copy variable data into ALLOCATED buffer. */
2932 isakmp_set_attr_v(buf
, type
, val
, len
)
2938 struct isakmp_data
*data
;
2940 data
= (struct isakmp_data
*)buf
;
2941 data
->type
= htons((u_int16_t
)type
| ISAKMP_GEN_TLV
);
2942 data
->lorv
= htons((u_int16_t
)len
);
2943 memcpy(data
+ 1, val
, len
);
2945 return buf
+ sizeof(*data
) + len
;
2948 /* copy fixed length data into ALLOCATED buffer. */
2950 isakmp_set_attr_l(buf
, type
, val
)
2955 struct isakmp_data
*data
;
2957 data
= (struct isakmp_data
*)buf
;
2958 data
->type
= htons((u_int16_t
)type
| ISAKMP_GEN_TV
);
2959 data
->lorv
= htons((u_int16_t
)val
);
2961 return buf
+ sizeof(*data
);
2964 /* add a variable data attribute to the buffer by reallocating it. */
2966 isakmp_add_attr_v(buf0
, type
, val
, len
)
2972 vchar_t
*buf
= NULL
;
2973 struct isakmp_data
*data
;
2977 tlen
= sizeof(*data
) + len
;
2981 buf
= vrealloc(buf0
, oldlen
+ tlen
);
2983 buf
= vmalloc(tlen
);
2985 plog(LLV_ERROR
, LOCATION
, NULL
,
2986 "failed to get a attribute buffer.\n");
2990 data
= (struct isakmp_data
*)(buf
->v
+ oldlen
);
2991 data
->type
= htons((u_int16_t
)type
| ISAKMP_GEN_TLV
);
2992 data
->lorv
= htons((u_int16_t
)len
);
2993 memcpy(data
+ 1, val
, len
);
2998 /* add a fixed data attribute to the buffer by reallocating it. */
3000 isakmp_add_attr_l(buf0
, type
, val
)
3005 vchar_t
*buf
= NULL
;
3006 struct isakmp_data
*data
;
3010 tlen
= sizeof(*data
);
3014 buf
= vrealloc(buf0
, oldlen
+ tlen
);
3016 buf
= vmalloc(tlen
);
3018 plog(LLV_ERROR
, LOCATION
, NULL
,
3019 "failed to get a attribute buffer.\n");
3023 data
= (struct isakmp_data
*)(buf
->v
+ oldlen
);
3024 data
->type
= htons((u_int16_t
)type
| ISAKMP_GEN_TV
);
3025 data
->lorv
= htons((u_int16_t
)val
);
3031 * calculate cookie and set.
3034 isakmp_newcookie(place
, remote
, local
)
3036 struct sockaddr_storage
*remote
;
3037 struct sockaddr_storage
*local
;
3039 vchar_t
*buf
= NULL
, *buf2
= NULL
;
3049 if (remote
->ss_family
!= local
->ss_family
) {
3050 plog(LLV_ERROR
, LOCATION
, NULL
,
3051 "address family mismatch, remote:%d local:%d\n",
3052 remote
->ss_family
, local
->ss_family
);
3055 switch (remote
->ss_family
) {
3057 alen
= sizeof(struct in_addr
);
3058 sa1
= (caddr_t
)&((struct sockaddr_in
*)remote
)->sin_addr
;
3059 sa2
= (caddr_t
)&((struct sockaddr_in
*)local
)->sin_addr
;
3063 alen
= sizeof(struct in_addr
);
3064 sa1
= (caddr_t
)&((struct sockaddr_in6
*)remote
)->sin6_addr
;
3065 sa2
= (caddr_t
)&((struct sockaddr_in6
*)local
)->sin6_addr
;
3069 plog(LLV_ERROR
, LOCATION
, NULL
,
3070 "invalid family: %d\n", remote
->ss_family
);
3073 blen
= (alen
+ sizeof(u_short
)) * 2
3074 + sizeof(time_t) + lcconf
->secret_size
;
3075 buf
= vmalloc(blen
);
3077 plog(LLV_ERROR
, LOCATION
, NULL
,
3078 "failed to get a cookie.\n");
3083 /* copy my address */
3084 memcpy(p
, sa1
, alen
);
3086 port
= ((struct sockaddr_in
*)remote
)->sin_port
;
3087 memcpy(p
, &port
, sizeof(u_short
));
3088 p
+= sizeof(u_short
);
3090 /* copy target address */
3091 memcpy(p
, sa2
, alen
);
3093 port
= ((struct sockaddr_in
*)local
)->sin_port
;
3094 memcpy(p
, &port
, sizeof(u_short
));
3095 p
+= sizeof(u_short
);
3099 memcpy(p
, (caddr_t
)&t
, sizeof(t
));
3102 /* copy random value */
3103 buf2
= eay_set_random(lcconf
->secret_size
);
3106 memcpy(p
, buf2
->v
, lcconf
->secret_size
);
3107 p
+= lcconf
->secret_size
;
3110 buf2
= eay_sha1_one(buf
);
3111 memcpy(place
, buf2
->v
, sizeof(cookie_t
));
3113 sa1
= val2str(place
, sizeof (cookie_t
));
3114 plog(LLV_DEBUG
, LOCATION
, NULL
, "new cookie:\n%s\n", sa1
);
3127 * save partner's(payload) data into phhandle.
3130 isakmp_p2ph(buf
, gen
)
3132 struct isakmp_gen
*gen
;
3134 /* XXX to be checked in each functions for logging. */
3136 plog(LLV_WARNING
, LOCATION
, NULL
,
3137 "ignore this payload, same payload type exist.\n");
3141 if (ntohs(gen
->len
) < sizeof(*gen
)) {
3142 plog(LLV_ERROR
, LOCATION
, NULL
,
3143 "ignore this payload, invalid payload len %d.\n",
3148 *buf
= vmalloc(ntohs(gen
->len
) - sizeof(*gen
));
3150 plog(LLV_ERROR
, LOCATION
, NULL
,
3151 "failed to get buffer.\n");
3154 memcpy((*buf
)->v
, gen
+ 1, (*buf
)->l
);
3160 isakmp_newmsgid2(iph1
)
3161 struct ph1handle
*iph1
;
3166 msgid2
= eay_random();
3167 } while (getph2bymsgid(iph1
, msgid2
));
3173 * set values into allocated buffer of isakmp header for phase 1
3176 set_isakmp_header(vbuf
, iph1
, nptype
, etype
, flags
, msgid
)
3178 struct ph1handle
*iph1
;
3184 struct isakmp
*isakmp
;
3186 if (vbuf
->l
< sizeof(*isakmp
))
3189 isakmp
= (struct isakmp
*)vbuf
->v
;
3191 memcpy(&isakmp
->i_ck
, &iph1
->index
.i_ck
, sizeof(cookie_t
));
3192 memcpy(&isakmp
->r_ck
, &iph1
->index
.r_ck
, sizeof(cookie_t
));
3193 isakmp
->np
= nptype
;
3194 isakmp
->v
= iph1
->version
;
3195 isakmp
->etype
= etype
;
3196 isakmp
->flags
= flags
;
3197 isakmp
->msgid
= msgid
;
3198 isakmp
->len
= htonl(vbuf
->l
);
3200 return vbuf
->v
+ sizeof(*isakmp
);
3204 * set values into allocated buffer of isakmp header for phase 1
3207 set_isakmp_header1(vbuf
, iph1
, nptype
)
3209 struct ph1handle
*iph1
;
3212 return set_isakmp_header (vbuf
, iph1
, nptype
, iph1
->etype
, iph1
->flags
, iph1
->msgid
);
3216 * set values into allocated buffer of isakmp header for phase 2
3219 set_isakmp_header2(vbuf
, iph2
, nptype
)
3221 struct ph2handle
*iph2
;
3224 return set_isakmp_header (vbuf
, iph2
->ph1
, nptype
, ISAKMP_ETYPE_QUICK
, iph2
->flags
, iph2
->msgid
);
3228 * set values into allocated buffer of isakmp payload.
3231 set_isakmp_payload(buf
, src
, nptype
)
3236 struct isakmp_gen
*gen
;
3239 plog(LLV_DEBUG
, LOCATION
, NULL
, "add payload of len %zu, next type %d\n",
3242 gen
= (struct isakmp_gen
*)p
;
3244 gen
->len
= htons(sizeof(*gen
) + src
->l
);
3246 memcpy(p
, src
->v
, src
->l
);
3257 case ISAKMP_ETYPE_IDENT
:
3259 case ISAKMP_ETYPE_AGG
:
3261 case ISAKMP_ETYPE_BASE
:
3274 case ISAKMP_ETYPE_QUICK
:
3282 #ifdef HAVE_PRINT_ISAKMP_C
3283 /* for print-isakmp.c */
3285 extern void isakmp_print
__P((const u_char
*, u_int
, const u_char
*));
3287 char *getname
__P((const u_char
*));
3289 char *getname6
__P((const u_char
*));
3291 int safeputchar
__P((int));
3294 * Return a name for the IP address pointed to by ap. This address
3295 * is assumed to be in network byte order.
3301 struct sockaddr_in addr
;
3302 static char ntop_buf
[NI_MAXHOST
];
3304 memset(&addr
, 0, sizeof(addr
));
3305 addr
.sin_len
= sizeof(struct sockaddr_in
);
3306 addr
.sin_family
= AF_INET
;
3307 memcpy(&addr
.sin_addr
, ap
, sizeof(addr
.sin_addr
));
3308 if (getnameinfo(&addr
, sizeof(addr
),
3309 ntop_buf
, sizeof(ntop_buf
), NULL
, 0,
3310 NI_NUMERICHOST
| niflags
))
3311 strlcpy(ntop_buf
, "?", sizeof(ntop_buf
));
3318 * Return a name for the IP6 address pointed to by ap. This address
3319 * is assumed to be in network byte order.
3325 struct sockaddr_in6 addr
;
3326 static char ntop_buf
[NI_MAXHOST
];
3328 memset(&addr
, 0, sizeof(addr
));
3329 addr
.sin6_len
= sizeof(struct sockaddr_in6
);
3330 addr
.sin6_family
= AF_INET6
;
3331 memcpy(&addr
.sin6_addr
, ap
, sizeof(addr
.sin6_addr
));
3332 if (getnameinfo(&addr
, addr
.sin6_len
,
3333 ntop_buf
, sizeof(ntop_buf
), NULL
, 0,
3334 NI_NUMERICHOST
| niflags
))
3335 strlcpy(ntop_buf
, "?", sizeof(ntop_buf
));
3347 ch
= (unsigned char)(c
& 0xff);
3348 if (c
< 0x80 && isprint(c
))
3349 return printf("%c", c
& 0xff);
3351 return printf("\\%03o", c
& 0xff);
3355 isakmp_printpacket(msg
, from
, my
, decoded
)
3357 struct sockaddr_storage
*from
;
3358 struct sockaddr_storage
*my
;
3364 char hostbuf
[NI_MAXHOST
];
3365 char portbuf
[NI_MAXSERV
];
3366 struct isakmp
*isakmp
;
3370 if (loglevel
< LLV_DEBUG
)
3374 plog(LLV_DEBUG
, LOCATION
, NULL
, "begin.\n");
3376 gettimeofday(&tv
, NULL
);
3377 s
= tv
.tv_sec
% 3600;
3378 printf("%02d:%02d.%06u ", s
/ 60, s
% 60, (u_int32_t
)tv
.tv_usec
);
3381 if (getnameinfo(from
, sysdep_sa_len((struct sockaddr
*)from
), hostbuf
, sizeof(hostbuf
),
3382 portbuf
, sizeof(portbuf
),
3383 NI_NUMERICHOST
| NI_NUMERICSERV
| niflags
)) {
3384 strlcpy(hostbuf
, "?", sizeof(hostbuf
));
3385 strlcpy(portbuf
, "?", sizeof(portbuf
));
3387 printf("%s:%s", hostbuf
, portbuf
);
3392 if (getnameinfo(my
, sysdep_sa_len((struct sockaddr
*)my
), hostbuf
, sizeof(hostbuf
),
3393 portbuf
, sizeof(portbuf
),
3394 NI_NUMERICHOST
| NI_NUMERICSERV
| niflags
)) {
3395 strlcpy(hostbuf
, "?", sizeof(hostbuf
));
3396 strlcpy(portbuf
, "?", sizeof(portbuf
));
3398 printf("%s:%s", hostbuf
, portbuf
);
3405 printf("(malloc fail)\n");
3409 isakmp
= (struct isakmp
*)buf
->v
;
3410 if (isakmp
->flags
& ISAKMP_FLAG_E
) {
3413 pad
= *(u_char
*)(buf
->v
+ buf
->l
- 1);
3414 if (buf
->l
< pad
&& 2 < vflag
)
3415 printf("(wrong padding)");
3417 isakmp
->flags
&= ~ISAKMP_FLAG_E
;
3421 snapend
= buf
->v
+ buf
->l
;
3422 isakmp_print(buf
->v
, buf
->l
, NULL
);
3430 #endif /*HAVE_PRINT_ISAKMP_C*/
3433 copy_ph1addresses(iph1
, rmconf
, remote
, local
)
3434 struct ph1handle
*iph1
;
3435 struct remoteconf
*rmconf
;
3436 struct sockaddr_storage
*remote
, *local
;
3438 u_short
*port
= NULL
;
3440 /* address portion must be grabbed from real remote address "remote" */
3441 iph1
->remote
= dupsaddr((struct sockaddr
*)remote
);
3442 if (iph1
->remote
== NULL
) {
3448 * if remote has no port # (in case of initiator - from ACQUIRE msg)
3449 * - if remote.conf specifies port #, use that
3450 * - if remote.conf does not, use 500
3451 * if remote has port # (in case of responder - from recvfrom(2))
3452 * respect content of "remote".
3454 switch (iph1
->remote
->ss_family
) {
3456 port
= &((struct sockaddr_in
*)iph1
->remote
)->sin_port
;
3459 *port
= ((struct sockaddr_in
*)rmconf
->remote
)->sin_port
;
3462 *port
= htons(PORT_ISAKMP
);
3466 port
= &((struct sockaddr_in6
*)iph1
->remote
)->sin6_port
;
3469 *port
= ((struct sockaddr_in6
*)rmconf
->remote
)->sin6_port
;
3472 *port
= htons(PORT_ISAKMP
);
3476 plog(LLV_ERROR
, LOCATION
, NULL
,
3477 "invalid family: %d\n", iph1
->remote
->ss_family
);
3483 iph1
->local
= getlocaladdr((struct sockaddr
*)iph1
->remote
);
3485 iph1
->local
= dupsaddr((struct sockaddr
*)local
);
3486 if (iph1
->local
== NULL
) {
3491 switch (iph1
->local
->ss_family
) {
3493 port
= &((struct sockaddr_in
*)iph1
->local
)->sin_port
;
3496 *port
= ((struct sockaddr_in
*)iph1
->local
)->sin_port
;
3499 *port
= getmyaddrsport(iph1
->local
);
3503 port
= &((struct sockaddr_in6
*)iph1
->local
)->sin6_port
;
3506 *port
= ((struct sockaddr_in6
*)iph1
->local
)->sin6_port
;
3509 *port
= getmyaddrsport(iph1
->local
);
3513 plog(LLV_ERROR
, LOCATION
, NULL
,
3514 "invalid family: %d\n", iph1
->local
->ss_family
);
3519 if ( port
!= NULL
&& *port
== htons(lcconf
->port_isakmp_natt
) ) {
3520 plog (LLV_DEBUG
, LOCATION
, NULL
, "Marking ports as changed\n");
3521 iph1
->natt_flags
|= NAT_ADD_NON_ESP_MARKER
;
3530 struct ph1handle
*iph1
;
3533 plog(LLV_ERROR
, LOCATION
, iph1
->remote
, "wrong state %u.\n",
3540 struct ph2handle
*iph2
;
3543 plog(LLV_ERROR
, LOCATION
, iph2
->ph1
->remote
, "wrong state %u.\n",
3549 log_ph1established(iph1
)
3550 const struct ph1handle
*iph1
;
3554 src
= racoon_strdup(saddr2str((struct sockaddr
*)iph1
->local
));
3555 dst
= racoon_strdup(saddr2str((struct sockaddr
*)iph1
->remote
));
3559 plog(LLV_INFO
, LOCATION
, NULL
,
3560 "ISAKMP-SA established %s-%s spi:%s\n",
3562 isakmp_pindex(&iph1
->index
, 0));
3563 EVT_PUSH(iph1
->local
, iph1
->remote
, EVTT_PHASE1_UP
, NULL
);
3564 if(!iph1
->rmconf
->mode_cfg
)
3565 EVT_PUSH(iph1
->local
, iph1
->remote
, EVTT_NO_ISAKMP_CFG
, NULL
);
3570 IPSECLOGASLMSG("IPSec Phase1 established (Initiated by %s).\n",
3571 (iph1
->side
== INITIATOR
)? "me" : "peer");
3576 struct payload_list
*
3577 isakmp_plist_append (struct payload_list
*plist
, vchar_t
*payload
, int payload_type
)
3580 plist
= racoon_malloc (sizeof (struct payload_list
));
3584 plist
->next
= racoon_malloc (sizeof (struct payload_list
));
3585 plist
->next
->prev
= plist
;
3586 plist
= plist
->next
;
3590 plist
->payload
= payload
;
3591 plist
->payload_type
= payload_type
;
3597 isakmp_plist_set_all (struct payload_list
**plist
, struct ph1handle
*iph1
)
3599 struct payload_list
*ptr
= *plist
, *first
;
3600 size_t tlen
= sizeof (struct isakmp
), n
= 0;
3601 vchar_t
*buf
= NULL
;
3604 /* Seek to the first item. */
3605 while (ptr
->prev
) ptr
= ptr
->prev
;
3608 /* Compute the whole length. */
3610 tlen
+= ptr
->payload
->l
+ sizeof (struct isakmp_gen
);
3614 buf
= vmalloc(tlen
);
3616 plog(LLV_ERROR
, LOCATION
, NULL
,
3617 "failed to get buffer to send.\n");
3623 p
= set_isakmp_header1(buf
, iph1
, ptr
->payload_type
);
3629 p
= set_isakmp_payload (p
, ptr
->payload
, ptr
->next
? ptr
->next
->payload_type
: ISAKMP_NPTYPE_NONE
);
3632 racoon_free (first
);
3633 /* ptr->prev = NULL; first = NULL; ... omitted. */
3648 frag_handler(iph1
, msg
, remote
, local
)
3649 struct ph1handle
*iph1
;
3651 struct sockaddr_storage
*remote
;
3652 struct sockaddr_storage
*local
;
3657 if (isakmp_frag_extract(iph1
, msg
) == 1) {
3658 if ((newmsg
= isakmp_frag_reassembly(iph1
)) == NULL
) {
3659 plog(LLV_ERROR
, LOCATION
, remote
,
3660 "Packet reassembly failed\n");
3664 /* simply reply if the packet was processed. */
3665 if (check_recvdpkt(remote
, local
, newmsg
) > 0) {
3666 IPSECLOGASLMSG("Received (reassembled) retransmitted packet from %s.\n",
3667 saddr2str((struct sockaddr
*)remote
));
3669 plog(LLV_NOTIFY
, LOCATION
, NULL
,
3670 "the reassembled packet is retransmitted by %s.\n",
3671 saddr2str((struct sockaddr
*)remote
));
3676 result
= isakmp_main(newmsg
, remote
, local
);
3686 script_hook(iph1
, script
)
3687 struct ph1handle
*iph1
;
3692 char addrstr
[IP_MAX
];
3693 char portstr
[PORT_MAX
];
3696 struct sockaddr_in
*sin
;
3700 iph1
->rmconf
== NULL
||
3701 iph1
->rmconf
->script
[script
] == NULL
)
3704 #ifdef ENABLE_HYBRID
3705 (void)isakmp_cfg_setenv(iph1
, &envp
, &envc
);
3709 sin
= (struct sockaddr_in
*)iph1
->local
;
3710 inet_ntop(sin
->sin_family
, &sin
->sin_addr
, addrstr
, IP_MAX
);
3711 snprintf(portstr
, sizeof(portstr
), "%d", ntohs(sin
->sin_port
));
3713 if (script_env_append(&envp
, &envc
, "LOCAL_ADDR", addrstr
) != 0) {
3714 plog(LLV_ERROR
, LOCATION
, NULL
, "Cannot set LOCAL_ADDR\n");
3718 if (script_env_append(&envp
, &envc
, "LOCAL_PORT", portstr
) != 0) {
3719 plog(LLV_ERROR
, LOCATION
, NULL
, "Cannot set LOCAL_PORT\n");
3724 if (iph1
->remote
!= NULL
) {
3725 sin
= (struct sockaddr_in
*)iph1
->remote
;
3726 inet_ntop(sin
->sin_family
, &sin
->sin_addr
, addrstr
, IP_MAX
);
3727 snprintf(portstr
, sizeof(portstr
), "%d", ntohs(sin
->sin_port
));
3729 if (script_env_append(&envp
, &envc
,
3730 "REMOTE_ADDR", addrstr
) != 0) {
3731 plog(LLV_ERROR
, LOCATION
, NULL
,
3732 "Cannot set REMOTE_ADDR\n");
3736 if (script_env_append(&envp
, &envc
,
3737 "REMOTE_PORT", portstr
) != 0) {
3738 plog(LLV_ERROR
, LOCATION
, NULL
,
3739 "Cannot set REMOTEL_PORT\n");
3745 if (privsep_script_exec(iph1
->rmconf
->script
[script
]->v
,
3747 plog(LLV_ERROR
, LOCATION
, NULL
,
3748 "Script %s execution failed\n", script_names
[script
]);
3750 if (script_exec(iph1
->rmconf
->script
[script
]->v
,
3752 plog(LLV_ERROR
, LOCATION
, NULL
,
3753 "Script %s execution failed\n", script_names
[script
]);
3757 for (c
= envp
; *c
; c
++)
3766 script_env_append(envp
, envc
, name
, value
)
3777 envitem_len
= strlen(name
) + 1 + strlen(value
) + 1;
3778 envitem
= racoon_malloc(envitem_len
);
3779 if (envitem
== NULL
) {
3780 plog(LLV_ERROR
, LOCATION
, NULL
,
3781 "Cannot allocate memory: %s\n", strerror(errno
));
3784 snprintf(envitem
, envitem_len
, "%s=%s", name
, value
);
3786 newenvc
= (*envc
) + 1;
3787 newenvp
= racoon_realloc(*envp
, newenvc
* sizeof(char *));
3788 if (newenvp
== NULL
) {
3789 plog(LLV_ERROR
, LOCATION
, NULL
,
3790 "Cannot allocate memory: %s\n", strerror(errno
));
3791 racoon_free(envitem
);
3795 newenvp
[newenvc
- 2] = envitem
;
3796 newenvp
[newenvc
- 1] = NULL
;
3804 script_exec(script
, name
, envp
)
3809 char *argv
[] = { NULL
, NULL
, NULL
};
3812 argv
[1] = script_names
[name
];
3817 execve(argv
[0], argv
, envp
);
3818 plog(LLV_ERROR
, LOCATION
, NULL
,
3819 "execve(\"%s\") failed: %s\n",
3820 argv
[0], strerror(errno
));
3824 plog(LLV_ERROR
, LOCATION
, NULL
,
3825 "Cannot fork: %s\n", strerror(errno
));
3837 struct ph1handle
*iph1
;
3839 vchar_t
*buf
= NULL
;
3840 struct sadb_msg
*msg
, *next
, *end
;
3842 struct sockaddr_storage
*src
, *dst
;
3843 caddr_t mhp
[SADB_EXT_MAX
+ 1];
3845 struct ph2handle
*iph2
;
3846 struct ph1handle
*new_iph1
;
3848 plog(LLV_INFO
, LOCATION
, NULL
,
3849 "purging ISAKMP-SA spi=%s.\n",
3850 isakmp_pindex(&(iph1
->index
), iph1
->msgid
));
3852 /* Mark as expired. */
3853 iph1
->status
= PHASE1ST_EXPIRED
;
3855 new_iph1
= ike_session_update_ph1_ph2tree(iph1
);
3858 * Delete all orphaned or binded to the deleting ph1handle phase2 SAs.
3859 * Keep all others phase2 SAs.
3861 buf
= pfkey_dump_sadb(SADB_SATYPE_UNSPEC
);
3863 plog(LLV_DEBUG
, LOCATION
, NULL
,
3864 "pfkey_dump_sadb returned nothing.\n");
3868 msg
= ALIGNED_CAST(struct sadb_msg
*)buf
->v
;
3869 end
= ALIGNED_CAST(struct sadb_msg
*)(buf
->v
+ buf
->l
);
3872 if ((msg
->sadb_msg_len
<< 3) < sizeof(*msg
))
3874 next
= ALIGNED_CAST(struct sadb_msg
*)((caddr_t
)msg
+ (msg
->sadb_msg_len
<< 3));
3875 if (msg
->sadb_msg_type
!= SADB_DUMP
) {
3880 if (pfkey_align(msg
, mhp
) || pfkey_check(mhp
)) {
3881 plog(LLV_ERROR
, LOCATION
, NULL
,
3882 "pfkey_check (%s)\n", ipsec_strerror());
3887 sa
= ALIGNED_CAST(struct sadb_sa
*)(mhp
[SADB_EXT_SA
]);
3889 !mhp
[SADB_EXT_ADDRESS_SRC
] ||
3890 !mhp
[SADB_EXT_ADDRESS_DST
]) {
3894 src
= ALIGNED_CAST(struct sockaddr_storage
*)PFKEY_ADDR_SADDR(mhp
[SADB_EXT_ADDRESS_SRC
]);
3895 dst
= ALIGNED_CAST(struct sockaddr_storage
*)PFKEY_ADDR_SADDR(mhp
[SADB_EXT_ADDRESS_DST
]);
3897 if (sa
->sadb_sa_state
!= SADB_SASTATE_LARVAL
&&
3898 sa
->sadb_sa_state
!= SADB_SASTATE_MATURE
&&
3899 sa
->sadb_sa_state
!= SADB_SASTATE_DYING
) {
3905 * check in/outbound SAs.
3906 * Select only SAs where src == local and dst == remote (outgoing)
3907 * or src == remote and dst == local (incoming).
3909 if ((CMPSADDR(iph1
->local
, src
) || CMPSADDR(iph1
->remote
, dst
)) &&
3910 (CMPSADDR(iph1
->local
, dst
) || CMPSADDR(iph1
->remote
, src
))) {
3915 proto_id
= pfkey2ipsecdoi_proto(msg
->sadb_msg_satype
);
3916 iph2
= getph2bysaidx(src
, dst
, proto_id
, sa
->sadb_sa_spi
);
3918 /* Check if there is another valid ISAKMP-SA */
3919 if (new_iph1
!= NULL
) {
3922 /* No handler... still send a pfkey_delete message, but log this !*/
3923 plog(LLV_INFO
, LOCATION
, NULL
,
3924 "Unknown IPsec-SA spi=%u, hmmmm?\n",
3925 ntohl(sa
->sadb_sa_spi
));
3929 * If we have a new ph1, do not purge IPsec-SAs binded
3930 * to a different ISAKMP-SA
3932 if (iph2
->ph1
!= NULL
&& iph2
->ph1
!= iph1
){
3937 /* If the ph2handle is established, do not purge IPsec-SA */
3938 if (iph2
->status
== PHASE2ST_ESTABLISHED
||
3939 iph2
->status
== PHASE2ST_EXPIRED
) {
3941 plog(LLV_INFO
, LOCATION
, NULL
,
3942 "keeping IPsec-SA spi=%u - found valid ISAKMP-SA spi=%s.\n",
3943 ntohl(sa
->sadb_sa_spi
),
3944 isakmp_pindex(&(new_iph1
->index
), new_iph1
->msgid
));
3952 pfkey_send_delete(lcconf
->sock_pfkey
,
3953 msg
->sadb_msg_satype
,
3955 src
, dst
, sa
->sadb_sa_spi
);
3957 /* delete a relative phase 2 handle. */
3965 plog(LLV_INFO
, LOCATION
, NULL
,
3966 "purged IPsec-SA spi=%u.\n",
3967 ntohl(sa
->sadb_sa_spi
));
3975 /* Mark the phase1 handler as EXPIRED */
3976 plog(LLV_INFO
, LOCATION
, NULL
,
3977 "purged ISAKMP-SA spi=%s.\n",
3978 isakmp_pindex(&(iph1
->index
), iph1
->msgid
));
3980 SCHED_KILL(iph1
->sce
);
3982 iph1
->sce
= sched_new(1, isakmp_ph1delete_stub
, iph1
);
3987 struct ph2handle
*iph2
;
3992 /* Delete the SPD entry if we generated it
3994 if (iph2
->generated_spidx
) {
3996 u_int64_t force_align
; // Wcast-align fix - force alignment
3997 struct policyindex spidx
;
3999 struct sockaddr_storage addr
;
4001 struct sockaddr_storage
*src
= iph2
->src
;
4002 struct sockaddr_storage
*dst
= iph2
->dst
;
4004 int idi2type
= 0;/* switch whether copy IDs into id[src,dst]. */
4006 plog(LLV_INFO
, LOCATION
, NULL
,
4007 "generated policy, deleting it.\n");
4009 memset(&u
.spidx
, 0, sizeof(u
.spidx
));
4010 iph2
->spidx_gen
= &u
.spidx
;
4012 /* make inbound policy */
4015 u
.spidx
.dir
= IPSEC_DIR_INBOUND
;
4016 u
.spidx
.ul_proto
= 0;
4019 * Note: code from get_proposal_r
4022 #define _XIDT(d) (ALIGNED_CAST(struct ipsecdoi_id_b *)((d)->v))->type
4025 * make destination address in spidx from either ID payload
4026 * or phase 1 address into a address in spidx.
4028 if (iph2
->id
!= NULL
4029 && (_XIDT(iph2
->id
) == IPSECDOI_ID_IPV4_ADDR
4030 || _XIDT(iph2
->id
) == IPSECDOI_ID_IPV6_ADDR
4031 || _XIDT(iph2
->id
) == IPSECDOI_ID_IPV4_ADDR_SUBNET
4032 || _XIDT(iph2
->id
) == IPSECDOI_ID_IPV6_ADDR_SUBNET
)) {
4033 /* get a destination address of a policy */
4034 error
= ipsecdoi_id2sockaddr(iph2
->id
, &u
.spidx
.dst
,
4035 &u
.spidx
.prefd
, &u
.spidx
.ul_proto
);
4041 * get scopeid from the SA address.
4042 * note that the phase 1 source address is used as
4043 * a destination address to search for a inbound
4044 * policy entry because rcoon is responder.
4046 if (_XIDT(iph2
->id
) == IPSECDOI_ID_IPV6_ADDR
) {
4048 setscopeid(&u
.spidx
.dst
, iph2
->src
)) != 0)
4053 if (_XIDT(iph2
->id
) == IPSECDOI_ID_IPV4_ADDR
4054 || _XIDT(iph2
->id
) == IPSECDOI_ID_IPV6_ADDR
)
4055 idi2type
= _XIDT(iph2
->id
);
4059 plog(LLV_DEBUG
, LOCATION
, NULL
,
4060 "get a destination address of SP index "
4061 "from phase1 address "
4062 "due to no ID payloads found "
4063 "OR because ID type is not address.\n");
4066 * copy the SOURCE address of IKE into the
4067 * DESTINATION address of the key to search the
4068 * SPD because the direction of policy is inbound.
4070 memcpy(&u
.spidx
.dst
, iph2
->src
, sysdep_sa_len((struct sockaddr
*)iph2
->src
));
4071 switch (u
.spidx
.dst
.ss_family
) {
4074 sizeof(struct in_addr
) << 3;
4079 sizeof(struct in6_addr
) << 3;
4088 /* make source address in spidx */
4089 if (iph2
->id_p
!= NULL
4090 && (_XIDT(iph2
->id_p
) == IPSECDOI_ID_IPV4_ADDR
4091 || _XIDT(iph2
->id_p
) == IPSECDOI_ID_IPV6_ADDR
4092 || _XIDT(iph2
->id_p
) == IPSECDOI_ID_IPV4_ADDR_SUBNET
4093 || _XIDT(iph2
->id_p
) == IPSECDOI_ID_IPV6_ADDR_SUBNET
)) {
4094 /* get a source address of inbound SA */
4095 error
= ipsecdoi_id2sockaddr(iph2
->id_p
, &u
.spidx
.src
,
4096 &u
.spidx
.prefs
, &u
.spidx
.ul_proto
);
4102 * get scopeid from the SA address.
4103 * for more detail, see above of this function.
4105 if (_XIDT(iph2
->id_p
) == IPSECDOI_ID_IPV6_ADDR
) {
4107 setscopeid(&u
.spidx
.src
, iph2
->dst
);
4113 /* make id[src,dst] if both ID types are IP address and same */
4114 if (_XIDT(iph2
->id_p
) == idi2type
4115 && u
.spidx
.dst
.ss_family
== u
.spidx
.src
.ss_family
) {
4117 dupsaddr((struct sockaddr
*)&u
.spidx
.dst
);
4119 dupsaddr((struct sockaddr
*)&u
.spidx
.src
);
4123 plog(LLV_DEBUG
, LOCATION
, NULL
,
4124 "get a source address of SP index "
4125 "from phase1 address "
4126 "due to no ID payloads found "
4127 "OR because ID type is not address.\n");
4129 /* see above comment. */
4130 memcpy(&u
.spidx
.src
, iph2
->dst
, sysdep_sa_len((struct sockaddr
*)iph2
->dst
));
4131 switch (u
.spidx
.src
.ss_family
) {
4134 sizeof(struct in_addr
) << 3;
4139 sizeof(struct in6_addr
) << 3;
4150 plog(LLV_DEBUG
, LOCATION
, NULL
,
4151 "get a src address from ID payload "
4152 "%s prefixlen=%u ul_proto=%u\n",
4153 saddr2str((struct sockaddr
*)&u
.spidx
.src
),
4154 u
.spidx
.prefs
, u
.spidx
.ul_proto
);
4155 plog(LLV_DEBUG
, LOCATION
, NULL
,
4156 "get dst address from ID payload "
4157 "%s prefixlen=%u ul_proto=%u\n",
4158 saddr2str((struct sockaddr
*)&u
.spidx
.dst
),
4159 u
.spidx
.prefd
, u
.spidx
.ul_proto
);
4162 * convert the ul_proto if it is 0
4163 * because 0 in ID payload means a wild card.
4165 if (u
.spidx
.ul_proto
== 0)
4166 u
.spidx
.ul_proto
= IPSEC_ULPROTO_ANY
;
4170 /* End of code from get_proposal_r
4173 if (pk_sendspddelete(iph2
) < 0) {
4174 plog(LLV_ERROR
, LOCATION
, NULL
,
4175 "pfkey spddelete(inbound) failed.\n");
4177 plog(LLV_DEBUG
, LOCATION
, NULL
,
4178 "pfkey spddelete(inbound) sent.\n");
4181 #ifdef HAVE_POLICY_FWD
4182 /* make forward policy if required */
4183 if (tunnel_mode_prop(iph2
->approval
)) {
4184 u
.spidx
.dir
= IPSEC_DIR_FWD
;
4185 if (pk_sendspddelete(iph2
) < 0) {
4186 plog(LLV_ERROR
, LOCATION
, NULL
,
4187 "pfkey spddelete(forward) failed.\n");
4189 plog(LLV_DEBUG
, LOCATION
, NULL
,
4190 "pfkey spddelete(forward) sent.\n");
4195 /* make outbound policy */
4198 u
.spidx
.dir
= IPSEC_DIR_OUTBOUND
;
4200 u
.spidx
.src
= u
.spidx
.dst
;
4202 pref
= u
.spidx
.prefs
;
4203 u
.spidx
.prefs
= u
.spidx
.prefd
;
4204 u
.spidx
.prefd
= pref
;
4206 if (pk_sendspddelete(iph2
) < 0) {
4207 plog(LLV_ERROR
, LOCATION
, NULL
,
4208 "pfkey spddelete(outbound) failed.\n");
4210 plog(LLV_DEBUG
, LOCATION
, NULL
,
4211 "pfkey spddelete(outbound) sent.\n");
4214 iph2
->spidx_gen
=NULL
;
4220 setscopeid(sp_addr0
, sa_addr0
)
4221 struct sockaddr_storage
*sp_addr0
, *sa_addr0
;
4223 struct sockaddr_in6
*sp_addr
, *sa_addr
;
4225 sp_addr
= (struct sockaddr_in6
*)sp_addr0
;
4226 sa_addr
= (struct sockaddr_in6
*)sa_addr0
;
4228 if (!IN6_IS_ADDR_LINKLOCAL(&sp_addr
->sin6_addr
)
4229 && !IN6_IS_ADDR_SITELOCAL(&sp_addr
->sin6_addr
)
4230 && !IN6_IS_ADDR_MULTICAST(&sp_addr
->sin6_addr
))
4233 /* this check should not be here ? */
4234 if (sa_addr
->sin6_family
!= AF_INET6
) {
4235 plog(LLV_ERROR
, LOCATION
, NULL
,
4236 "can't get scope ID: family mismatch\n");
4240 if (!IN6_IS_ADDR_LINKLOCAL(&sa_addr
->sin6_addr
)) {
4241 plog(LLV_ERROR
, LOCATION
, NULL
,
4242 "scope ID is not supported except of lladdr.\n");
4246 sp_addr
->sin6_scope_id
= sa_addr
->sin6_scope_id
;
4253 isakmp_plist_append_initial_contact (iph1
, plist
)
4254 struct ph1handle
*iph1
;
4255 struct payload_list
*plist
;
4257 if (!iph1
->is_rekey
&& iph1
->rmconf
->ini_contact
&& !getcontacted(iph1
->remote
)) {
4258 vchar_t
*notp_ini
= NULL
;
4259 struct isakmp_pl_n np
, *nptr
;
4262 np
.doi
= htonl(iph1
->rmconf
->doitype
);
4263 np
.proto_id
= IPSECDOI_PROTO_ISAKMP
;
4264 np
.spi_size
= sizeof(isakmp_index
);
4265 np
.type
= htons(ISAKMP_NTYPE_INITIAL_CONTACT
);
4266 if ((notp_ini
= vmalloc(sizeof(struct isakmp_pl_n
) - sizeof(struct isakmp_gen
)
4267 + sizeof(isakmp_index
)))) {
4269 memcpy(notp_ini
->v
, &nptr
->doi
, sizeof(struct isakmp_pl_n
) - sizeof(struct isakmp_gen
));
4270 cptr
= notp_ini
->v
+ sizeof(struct isakmp_pl_n
) - sizeof(struct isakmp_gen
);
4271 memcpy(cptr
, &iph1
->index
, sizeof(isakmp_index
));
4272 plist
= isakmp_plist_append(plist
, notp_ini
, ISAKMP_NPTYPE_N
);
4273 plog(LLV_DEBUG2
, LOCATION
, iph1
->remote
,
4274 "added initial-contact payload.\n");
4276 /* insert a node into contacted list. */
4277 if (inscontacted(iph1
->remote
) == -1) {
4278 plog(LLV_ERROR
, LOCATION
, iph1
->remote
,
4279 "failed to add contacted list.\n");
4284 plog(LLV_ERROR
, LOCATION
, iph1
->remote
,
4285 "failed to allocate notification payload.\n");
4289 plog(LLV_DEBUG
, LOCATION
, iph1
->remote
,
4290 "failed to add initial-contact payload: rekey %d, ini-contact %d, contacted %d.\n",
4291 iph1
->is_rekey
? 1:0, iph1
->rmconf
->ini_contact
, getcontacted(iph1
->remote
)? 1:0);