2 * Copyright (c) 2008 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
23 #include <sys/param.h>
24 #include <sys/types.h>
25 #include <sys/socket.h>
35 #include "isakmp_var.h"
37 #include "ike_session.h"
40 #include "nattraversal.h"
43 #include "ipsec_doi.h"
44 #include "ipsecSessionTracer.h"
45 #include "ipsecMessageTracer.h"
46 #include "isakmp_inf.h"
47 #include "localconf.h"
48 #include "remoteconf.h"
49 #include "vpn_control.h"
52 #include "power_mgmt.h"
54 #define GET_SAMPLE_PERIOD(s,m) do { \
64 const char *ike_session_stopped_by_vpn_disconnect
= "Stopped by VPN disconnect";
65 const char *ike_session_stopped_by_flush
= "Stopped by Flush";
66 const char *ike_session_stopped_by_idle
= "Stopped by Idle";
67 const char *ike_session_stopped_by_xauth_timeout
= "Stopped by XAUTH timeout";
68 const char *ike_session_stopped_by_sleepwake
= "Stopped by Sleep-Wake";
69 const char *ike_session_stopped_by_assert
= "Stopped by Assert";
71 static LIST_HEAD(_ike_session_tree_
, ike_session
) ike_session_tree
= { NULL
};
73 static ike_session_t
*
74 new_ike_session (ike_session_id_t
*id
)
76 ike_session_t
*session
;
79 plog(LLV_DEBUG2
, LOCATION
, NULL
, "invalid parameters in %s.\n", __FUNCTION__
);
83 plog(LLV_DEBUG
, LOCATION
, NULL
, "new parent session.\n");
84 session
= racoon_calloc(1, sizeof(*session
));
86 bzero(session
, sizeof(*session
));
87 memcpy(&session
->session_id
, id
, sizeof(*id
));
88 LIST_INIT(&session
->ikev1_state
.ph1tree
);
89 LIST_INIT(&session
->ikev1_state
.ph2tree
);
90 LIST_INSERT_HEAD(&ike_session_tree
, session
, chain
);
91 session
->version
= IKE_VERSION_1
; // hard-coded for now
92 IPSECSESSIONTRACERSTART(session
);
98 free_ike_session (ike_session_t
*session
)
100 int is_failure
= TRUE
;
102 SCHED_KILL(session
->traffic_monitor
.sc_mon
);
103 SCHED_KILL(session
->traffic_monitor
.sc_idle
);
104 SCHED_KILL(session
->sc_xauth
);
105 if (session
->start_timestamp
.tv_sec
|| session
->start_timestamp
.tv_usec
) {
106 if (!(session
->stop_timestamp
.tv_sec
|| session
->stop_timestamp
.tv_usec
)) {
107 gettimeofday(&session
->stop_timestamp
, NULL
);
109 if (session
->term_reason
!= ike_session_stopped_by_vpn_disconnect
||
110 session
->term_reason
!= ike_session_stopped_by_flush
||
111 session
->term_reason
!= ike_session_stopped_by_idle
) {
114 IPSECSESSIONTRACERSTOP(session
,
116 session
->term_reason
);
118 // do MessageTracer cleanup here
119 plog(LLV_DEBUG
, LOCATION
, NULL
,
120 "Freeing IKE-Session to %s.\n",
121 saddr2str((struct sockaddr
*)&session
->session_id
.remote
));
122 LIST_REMOVE(session
, chain
);
123 racoon_free(session
);
128 ike_session_get_established_or_negoing_ph1 (ike_session_t
*session
)
130 struct ph1handle
*p
, *iph1
= NULL
;
133 plog(LLV_DEBUG2
, LOCATION
, NULL
, "invalid parameters in %s.\n", __FUNCTION__
);
137 // look for the most mature ph1 under the session
138 for (p
= LIST_FIRST(&session
->ikev1_state
.ph1tree
); p
; p
= LIST_NEXT(p
, ph1ofsession_chain
)) {
139 if (!p
->is_dying
&& p
->status
>= PHASE1ST_START
&& p
->status
<= PHASE1ST_ESTABLISHED
) {
140 if (!iph1
|| p
->status
> iph1
->status
) {
142 } else if (iph1
&& p
->status
== iph1
->status
) {
143 // TODO: pick better one based on farthest rekey/expiry remaining
152 ike_session_get_established_ph1 (ike_session_t
*session
)
157 plog(LLV_DEBUG2
, LOCATION
, NULL
, "invalid parameters in %s.\n", __FUNCTION__
);
161 for (p
= LIST_FIRST(&session
->ikev1_state
.ph1tree
); p
; p
= LIST_NEXT(p
, ph1ofsession_chain
)) {
162 if (!p
->is_dying
&& p
->status
== PHASE1ST_ESTABLISHED
) {
171 ike_session_init (void)
173 LIST_INIT(&ike_session_tree
);
177 ike_session_get_rekey_lifetime (int local_spi_is_higher
, u_int expiry_lifetime
)
179 u_int rekey_lifetime
= expiry_lifetime
/ 10;
181 if (rekey_lifetime
) {
182 if (local_spi_is_higher
) {
183 return (rekey_lifetime
* 9);
185 return (rekey_lifetime
* 8);
188 if (local_spi_is_higher
) {
189 rekey_lifetime
= expiry_lifetime
- 1;
191 rekey_lifetime
= expiry_lifetime
- 2;
194 if (rekey_lifetime
< expiry_lifetime
) {
195 return (rekey_lifetime
);
200 // TODO: optimize this mess later
202 ike_session_get_session (struct sockaddr
*local
,
203 struct sockaddr
*remote
,
208 ike_session_id_t id_default
;
209 ike_session_id_t id_floated_default
;
210 ike_session_id_t id_wop
;
211 ike_session_t
*best_match
= NULL
;
212 u_int16_t remote_port
;
213 int is_isakmp_remote_port
;
215 if (!local
|| !remote
) {
216 plog(LLV_DEBUG2
, LOCATION
, NULL
, "invalid parameters in %s.\n", __FUNCTION__
);
220 remote_port
= extract_port(remote
);
221 if (remote_port
&& remote_port
!= PORT_ISAKMP
&& remote_port
!= PORT_ISAKMP_NATT
) {
222 is_isakmp_remote_port
= 0;
224 is_isakmp_remote_port
= 1;
227 /* we will try a couple of matches first: if the exact id isn't found, then we'll try for an id that has zero'd ports */
228 bzero(&id
, sizeof(id
));
229 bzero(&id_default
, sizeof(id_default
));
230 bzero(&id_floated_default
, sizeof(id_floated_default
));
231 bzero(&id_wop
, sizeof(id_wop
));
232 if (local
->sa_family
== AF_INET
) {
233 memcpy(&id
.local
, local
, sizeof(struct sockaddr_in
));
234 memcpy(&id_default
.local
, local
, sizeof(struct sockaddr_in
));
235 memcpy(&id_floated_default
.local
, local
, sizeof(struct sockaddr_in
));
236 memcpy(&id_wop
.local
, local
, sizeof(struct sockaddr_in
));
237 } else if (local
->sa_family
== AF_INET6
) {
238 memcpy(&id
.local
, local
, sizeof(struct sockaddr_in6
));
239 memcpy(&id_default
.local
, local
, sizeof(struct sockaddr_in6
));
240 memcpy(&id_floated_default
.local
, local
, sizeof(struct sockaddr_in6
));
241 memcpy(&id_wop
.local
, local
, sizeof(struct sockaddr_in6
));
243 set_port((struct sockaddr
*)&id_default
.local
, PORT_ISAKMP
);
244 set_port((struct sockaddr
*)&id_floated_default
.local
, PORT_ISAKMP_NATT
);
245 set_port((struct sockaddr
*)&id_wop
.local
, 0);
246 if (remote
->sa_family
== AF_INET
) {
247 memcpy(&id
.remote
, remote
, sizeof(struct sockaddr_in
));
248 memcpy(&id_default
.remote
, remote
, sizeof(struct sockaddr_in
));
249 memcpy(&id_floated_default
.remote
, remote
, sizeof(struct sockaddr_in
));
250 memcpy(&id_wop
.remote
, remote
, sizeof(struct sockaddr_in
));
251 } else if (remote
->sa_family
== AF_INET6
) {
252 memcpy(&id
.remote
, remote
, sizeof(struct sockaddr_in6
));
253 memcpy(&id_default
.remote
, remote
, sizeof(struct sockaddr_in6
));
254 memcpy(&id_floated_default
.remote
, remote
, sizeof(struct sockaddr_in6
));
255 memcpy(&id_wop
.remote
, remote
, sizeof(struct sockaddr_in6
));
257 set_port((struct sockaddr
*)&id_default
.remote
, PORT_ISAKMP
);
258 set_port((struct sockaddr
*)&id_floated_default
.remote
, PORT_ISAKMP_NATT
);
259 set_port((struct sockaddr
*)&id_wop
.remote
, 0);
261 plog(LLV_DEBUG
, LOCATION
, local
,
262 "start search for IKE-Session. target %s.\n",
265 for (p
= LIST_FIRST(&ike_session_tree
); p
; p
= LIST_NEXT(p
, chain
)) {
266 plog(LLV_DEBUG
, LOCATION
, local
,
267 "still search for IKE-Session. this %s.\n",
268 saddr2str((struct sockaddr
*)&p
->session_id
.remote
));
270 if (memcmp(&p
->session_id
, &id
, sizeof(id
)) == 0) {
271 plog(LLV_DEBUG
, LOCATION
, local
,
272 "Pre-existing IKE-Session to %s. case 1.\n",
275 } else if (is_isakmp_remote_port
&& memcmp(&p
->session_id
, &id_default
, sizeof(id_default
)) == 0) {
276 plog(LLV_DEBUG
, LOCATION
, local
,
277 "Pre-existing IKE-Session to %s. case 2.\n",
280 } else if (is_isakmp_remote_port
&& p
->ports_floated
&& memcmp(&p
->session_id
, &id_floated_default
, sizeof(id_floated_default
)) == 0) {
281 plog(LLV_DEBUG
, LOCATION
, local
,
282 "Pre-existing IKE-Session to %s. case 3.\n",
285 } else if (is_isakmp_remote_port
&& memcmp(&p
->session_id
, &id_wop
, sizeof(id_wop
)) == 0) {
290 plog(LLV_DEBUG
, LOCATION
, local
,
291 "Best-match IKE-Session to %s.\n",
292 saddr2str((struct sockaddr
*)&best_match
->session_id
.remote
));
295 if (alloc_if_absent
) {
296 plog(LLV_DEBUG
, LOCATION
, local
,
297 "New IKE-Session to %s.\n",
298 saddr2str((struct sockaddr
*)&id
.remote
));
299 return new_ike_session(&id
);
306 ike_session_init_traffic_cop_params (struct ph1handle
*iph1
)
310 (!iph1
->rmconf
->idle_timeout
&& !iph1
->rmconf
->dpd_interval
)) {
314 if (!iph1
->parent_session
->traffic_monitor
.interv_idle
) {
315 iph1
->parent_session
->traffic_monitor
.interv_idle
= iph1
->rmconf
->idle_timeout
;
317 if (!iph1
->parent_session
->traffic_monitor
.dir_idle
) {
318 iph1
->parent_session
->traffic_monitor
.dir_idle
= iph1
->rmconf
->idle_timeout_dir
;
321 if (!iph1
->parent_session
->traffic_monitor
.interv_mon
) {
322 int min_period
, max_period
, sample_period
= 0;
324 /* calculate the sampling interval... half the smaller interval */
325 if (iph1
->rmconf
->dpd_interval
&&
326 (iph1
->rmconf
->dpd_algo
== DPD_ALGO_INBOUND_DETECT
||
327 iph1
->rmconf
->dpd_algo
== DPD_ALGO_BLACKHOLE_DETECT
)) {
328 // when certain types of dpd are enabled
329 min_period
= MIN(iph1
->rmconf
->dpd_interval
, iph1
->rmconf
->idle_timeout
);
330 max_period
= MAX(iph1
->rmconf
->dpd_interval
, iph1
->rmconf
->idle_timeout
);
331 } else if (iph1
->rmconf
->idle_timeout
) {
332 min_period
= max_period
= iph1
->rmconf
->idle_timeout
;
334 // DPD_ALGO_DEFAULT is configured and there's no idle timeout... we don't need to monitor traffic
338 GET_SAMPLE_PERIOD(sample_period
, min_period
);
340 GET_SAMPLE_PERIOD(sample_period
, max_period
);
342 iph1
->parent_session
->traffic_monitor
.interv_mon
= sample_period
;
347 ike_session_link_ph1_to_session (struct ph1handle
*iph1
)
349 ike_session_t
*session
;
352 plog(LLV_DEBUG2
, LOCATION
, NULL
, "invalid parameters in %s.\n", __FUNCTION__
);
356 session
= ike_session_get_session(iph1
->local
, iph1
->remote
, TRUE
);
358 plog(LLV_DEBUG2
, LOCATION
, NULL
, "failed to get session in %s.\n", __FUNCTION__
);
363 if (iph1
->parent_session
) {
364 if (session
== iph1
->parent_session
) {
367 // undo previous session
368 if (ike_session_unlink_ph1_from_session(iph1
) == 0) {
369 plog(LLV_DEBUG2
, LOCATION
, NULL
, "failed to unlink ph1 in %s.\n", __FUNCTION__
);
370 free_ike_session(session
);
374 gettimeofday(&session
->start_timestamp
, NULL
);
378 if (iph1
->started_by_api
) {
379 session
->is_cisco_ipsec
= 1;
380 session
->is_l2tpvpn_ipsec
= 0;
381 session
->is_btmm_ipsec
= 0;
383 iph1
->parent_session
= session
;
384 LIST_INSERT_HEAD(&session
->ikev1_state
.ph1tree
, iph1
, ph1ofsession_chain
);
385 session
->ikev1_state
.active_ph1cnt
++;
386 if ((!session
->ikev1_state
.ph1cnt
&&
387 iph1
->side
== INITIATOR
) ||
388 iph1
->started_by_api
) {
389 // client initiates the first phase1 or, is started by controller api
390 session
->is_client
= 1;
392 if (session
->established
&&
393 session
->ikev1_state
.ph1cnt
) {
396 session
->ikev1_state
.ph1cnt
++;
397 ike_session_init_traffic_cop_params(iph1
);
403 ike_session_update_mode (struct ph2handle
*iph2
)
405 if (!iph2
|| !iph2
->parent_session
) {
409 // exit early if we already detected cisco-ipsec
410 if (iph2
->parent_session
->is_cisco_ipsec
) {
414 if (iph2
->approval
) {
415 if (!ipsecdoi_any_transportmode(iph2
->approval
)) {
416 // cisco & btmm ipsec are pure tunnel-mode (but cisco ipsec is detected by ph1)
417 iph2
->parent_session
->is_cisco_ipsec
= 0;
418 iph2
->parent_session
->is_l2tpvpn_ipsec
= 0;
419 iph2
->parent_session
->is_btmm_ipsec
= 1;
421 } else if (ipsecdoi_transportmode(iph2
->approval
)) {
422 iph2
->parent_session
->is_cisco_ipsec
= 0;
423 iph2
->parent_session
->is_l2tpvpn_ipsec
= 1;
424 iph2
->parent_session
->is_btmm_ipsec
= 0;
427 } else if (iph2
->proposal
) {
428 if (!ipsecdoi_any_transportmode(iph2
->proposal
)) {
429 // cisco & btmm ipsec are pure tunnel-mode (but cisco ipsec is detected by ph1)
430 iph2
->parent_session
->is_cisco_ipsec
= 0;
431 iph2
->parent_session
->is_l2tpvpn_ipsec
= 0;
432 iph2
->parent_session
->is_btmm_ipsec
= 1;
434 } else if (ipsecdoi_transportmode(iph2
->proposal
)) {
435 iph2
->parent_session
->is_cisco_ipsec
= 0;
436 iph2
->parent_session
->is_l2tpvpn_ipsec
= 1;
437 iph2
->parent_session
->is_btmm_ipsec
= 0;
444 ike_session_cleanup_xauth_timeout (void *arg
)
446 ike_session_t
*session
= (ike_session_t
*)arg
;
448 SCHED_KILL(session
->sc_xauth
);
449 // if there are no more established ph2s, start a timer to teardown the session
450 if (!ike_session_has_established_ph2(session
)) {
451 ike_session_cleanup(session
, ike_session_stopped_by_xauth_timeout
);
453 session
->sc_xauth
= sched_new(300 /* 5 mins */,
454 ike_session_cleanup_xauth_timeout
,
460 ike_session_link_ph2_to_session (struct ph2handle
*iph2
)
462 struct sockaddr
*local
;
463 struct sockaddr
*remote
;
464 ike_session_t
*session
;
467 plog(LLV_DEBUG2
, LOCATION
, NULL
, "invalid parameters in %s.\n", __FUNCTION__
);
474 session
= ike_session_get_session(local
, remote
, TRUE
);
476 plog(LLV_DEBUG2
, LOCATION
, NULL
, "failed to get session in %s.\n", __FUNCTION__
);
481 if (iph2
->parent_session
) {
482 if (session
== iph2
->parent_session
) {
485 // undo previous session
486 if (ike_session_unlink_ph2_from_session(iph2
) == 0) {
487 plog(LLV_DEBUG2
, LOCATION
, NULL
, "failed to unlink ph2 in %s.\n", __FUNCTION__
);
488 free_ike_session(session
);
493 iph2
->parent_session
= session
;
494 LIST_INSERT_HEAD(&session
->ikev1_state
.ph2tree
, iph2
, ph2ofsession_chain
);
495 session
->ikev1_state
.active_ph2cnt
++;
496 if (!session
->ikev1_state
.ph2cnt
&&
497 iph2
->side
== INITIATOR
) {
498 // client initiates the first phase2
499 session
->is_client
= 1;
501 if (session
->established
&&
502 session
->ikev1_state
.ph2cnt
) {
505 session
->ikev1_state
.ph2cnt
++;
507 ike_session_update_mode(iph2
);
513 ike_session_unlink_ph1_from_session (struct ph1handle
*iph1
)
515 ike_session_t
*session
;
517 if (!iph1
|| !iph1
->parent_session
) {
518 plog(LLV_DEBUG2
, LOCATION
, NULL
, "invalid parameters in %s.\n", __FUNCTION__
);
522 if (LIST_FIRST(&iph1
->ph2tree
)) {
523 // reparent any phase2 that may be hanging on to this phase1
524 ike_session_update_ph1_ph2tree(iph1
);
527 session
= iph1
->parent_session
;
528 LIST_REMOVE(iph1
, ph1ofsession_chain
);
529 iph1
->parent_session
= NULL
;
530 session
->ikev1_state
.active_ph1cnt
--;
531 if (session
->ikev1_state
.active_ph1cnt
== 0 && session
->ikev1_state
.active_ph2cnt
== 0) {
532 session
->is_dying
= 1;
533 free_ike_session(session
);
540 ike_session_unlink_ph2_from_session (struct ph2handle
*iph2
)
542 ike_session_t
*session
;
544 if (!iph2
|| !iph2
->parent_session
) {
545 plog(LLV_DEBUG2
, LOCATION
, NULL
, "invalid parameters in %s.\n", __FUNCTION__
);
549 LIST_REMOVE(iph2
, ph2ofsession_chain
);
550 session
= iph2
->parent_session
;
551 iph2
->parent_session
= NULL
;
552 session
->ikev1_state
.active_ph2cnt
--;
553 if (session
->ikev1_state
.active_ph1cnt
== 0 && session
->ikev1_state
.active_ph2cnt
== 0) {
554 session
->is_dying
= 1;
555 free_ike_session(session
);
562 ike_session_has_other_established_ph1 (ike_session_t
*session
, struct ph1handle
*iph1
)
570 for (p
= LIST_FIRST(&session
->ikev1_state
.ph1tree
); p
; p
= LIST_NEXT(p
, ph1ofsession_chain
)) {
571 if (iph1
!= p
&& !p
->is_dying
) {
572 if (p
->status
== PHASE1ST_ESTABLISHED
&& p
->sce_rekey
) {
582 ike_session_has_other_negoing_ph1 (ike_session_t
*session
, struct ph1handle
*iph1
)
587 plog(LLV_DEBUG2
, LOCATION
, NULL
, "invalid parameters in %s.\n", __FUNCTION__
);
591 for (p
= LIST_FIRST(&session
->ikev1_state
.ph1tree
); p
; p
= LIST_NEXT(p
, ph1ofsession_chain
)) {
592 if (iph1
!= p
&& !p
->is_dying
) {
593 if (p
->status
>= PHASE1ST_START
&& p
->status
<= PHASE1ST_ESTABLISHED
) {
603 ike_session_has_other_established_ph2 (ike_session_t
*session
, struct ph2handle
*iph2
)
608 plog(LLV_DEBUG2
, LOCATION
, NULL
, "invalid parameters in %s.\n", __FUNCTION__
);
612 for (p
= LIST_FIRST(&session
->ikev1_state
.ph2tree
); p
; p
= LIST_NEXT(p
, ph2ofsession_chain
)) {
613 if (iph2
!= p
&& !p
->is_dying
&& iph2
->spid
== p
->spid
) {
614 if (p
->status
== PHASE2ST_ESTABLISHED
) {
624 ike_session_has_other_negoing_ph2 (ike_session_t
*session
, struct ph2handle
*iph2
)
629 plog(LLV_DEBUG2
, LOCATION
, NULL
, "invalid parameters in %s.\n", __FUNCTION__
);
633 for (p
= LIST_FIRST(&session
->ikev1_state
.ph2tree
); p
; p
= LIST_NEXT(p
, ph2ofsession_chain
)) {
634 plog(LLV_DEBUG2
, LOCATION
, NULL
, "%s: ph2 sub spid %d, db spid %d\n", __FUNCTION__
, iph2
->spid
, p
->spid
);
635 if (iph2
!= p
&& !p
->is_dying
&& iph2
->spid
== p
->spid
) {
636 if (p
->status
>= PHASE2ST_START
&& p
->status
<= PHASE2ST_ESTABLISHED
) {
646 ike_session_unbindph12_from_ph1 (struct ph1handle
*iph1
)
648 struct ph2handle
*p
, *next
;
650 for (p
= LIST_FIRST(&iph1
->ph2tree
); p
; p
= next
) {
651 // take next pointer now, since unbind and rebind may change the underlying ph2tree list
652 next
= LIST_NEXT(p
, ph1bind
);
658 ike_session_rebindph12_from_old_ph1_to_new_ph1 (struct ph1handle
*old_iph1
,
659 struct ph1handle
*new_iph1
)
661 struct ph2handle
*p
, *next
;
663 if (old_iph1
== new_iph1
|| !old_iph1
|| !new_iph1
) {
664 plog(LLV_DEBUG2
, LOCATION
, NULL
, "invalid parameters in %s.\n", __FUNCTION__
);
668 if (old_iph1
->parent_session
!= new_iph1
->parent_session
) {
669 plog(LLV_DEBUG2
, LOCATION
, NULL
, "invalid parent sessions in %s.\n", __FUNCTION__
);
673 for (p
= LIST_FIRST(&old_iph1
->ph2tree
); p
; p
= next
) {
674 // take next pointer now, since rebind may change the underlying ph2tree list
675 next
= LIST_NEXT(p
, ph1bind
);
676 if (p
->parent_session
!= new_iph1
->parent_session
) {
677 plog(LLV_ERROR
, LOCATION
, NULL
, "mismatched parent session in ph1bind replacement.\n");
679 if (p
->ph1
== new_iph1
) {
680 plog(LLV_ERROR
, LOCATION
, NULL
, "same phase1 in ph1bind replacement in %s.\n",__FUNCTION__
);
682 rebindph12(new_iph1
, p
);
687 ike_session_verify_ph2_parent_session (struct ph2handle
*iph2
)
690 plog(LLV_DEBUG2
, LOCATION
, NULL
, "invalid parameters in %s.\n", __FUNCTION__
);
694 if (!iph2
->parent_session
) {
695 plog(LLV_DEBUG
, LOCATION
, NULL
, "NULL parent session.\n");
696 if (ike_session_link_ph2_to_session(iph2
)) {
697 plog(LLV_DEBUG
, LOCATION
, NULL
, "NULL parent session... still failed to link to session.\n");
698 // failed to bind ph2 to session
706 ike_session_update_ph1_ph2tree (struct ph1handle
*iph1
)
708 struct ph1handle
*new_iph1
= NULL
;
711 plog(LLV_DEBUG2
, LOCATION
, NULL
, "invalid parameters in %s.\n", __FUNCTION__
);
715 if (iph1
->parent_session
) {
716 new_iph1
= ike_session_get_established_ph1(iph1
->parent_session
);
719 plog(LLV_DEBUG2
, LOCATION
, NULL
, "no ph1bind replacement found. NULL ph1.\n");
720 ike_session_unbindph12_from_ph1(iph1
);
721 } else if (iph1
== new_iph1
) {
722 plog(LLV_DEBUG2
, LOCATION
, NULL
, "no ph1bind replacement found. same ph1.\n");
723 ike_session_unbindph12_from_ph1(iph1
);
725 ike_session_rebindph12_from_old_ph1_to_new_ph1(iph1
, new_iph1
);
728 plog(LLV_DEBUG2
, LOCATION
, NULL
, "invalid parent session in %s.\n", __FUNCTION__
);
734 ike_session_update_ph2_ph1bind (struct ph2handle
*iph2
)
736 struct ph1handle
*iph1
;
739 plog(LLV_DEBUG2
, LOCATION
, NULL
, "invalid parameters in %s.\n", __FUNCTION__
);
743 iph1
= ike_session_get_established_ph1(iph2
->parent_session
);
744 if (iph1
&& iph2
->ph1
&& iph1
!= iph2
->ph1
) {
745 rebindph12(iph1
, iph2
);
746 } else if (iph1
&& !iph2
->ph1
) {
747 bindph12(iph1
, iph2
);
754 ike_session_ikev1_float_ports (struct ph1handle
*iph1
)
756 struct sockaddr
*local
, *remote
;
759 if (iph1
->parent_session
) {
760 local
= (struct sockaddr
*)&iph1
->parent_session
->session_id
.local
;
761 remote
= (struct sockaddr
*)&iph1
->parent_session
->session_id
.remote
;
763 set_port(local
, extract_port(iph1
->local
));
764 set_port(remote
, extract_port(iph1
->remote
));
765 iph1
->parent_session
->ports_floated
= 1;
767 for (p
= LIST_FIRST(&iph1
->parent_session
->ikev1_state
.ph2tree
); p
; p
= LIST_NEXT(p
, ph2ofsession_chain
)) {
772 set_port(local
, extract_port(iph1
->local
));
773 set_port(remote
, extract_port(iph1
->remote
));
776 plog(LLV_DEBUG2
, LOCATION
, NULL
, "invalid parent session in %s.\n", __FUNCTION__
);
781 ike_session_traffic_cop (void *arg
)
783 ike_session_t
*session
= (__typeof__(session
))arg
;
786 (session
->established
&& !session
->stopped_by_vpn_controller
&& !session
->stop_timestamp
.tv_sec
&& !session
->stop_timestamp
.tv_usec
)) {
787 SCHED_KILL(session
->traffic_monitor
.sc_mon
);
788 /* get traffic query from kernel */
789 if (pk_sendget_inbound_sastats(session
) < 0) {
791 plog(LLV_DEBUG2
, LOCATION
, NULL
, "pk_sendget_inbound_sastats failed in %s.\n", __FUNCTION__
);
793 if (pk_sendget_outbound_sastats(session
) < 0) {
795 plog(LLV_DEBUG2
, LOCATION
, NULL
, "pk_sendget_outbound_sastats failed in %s.\n", __FUNCTION__
);
797 session
->traffic_monitor
.sc_mon
= sched_new(session
->traffic_monitor
.interv_mon
,
798 ike_session_traffic_cop
,
802 plog(LLV_DEBUG2
, LOCATION
, NULL
, "invalid parameters in %s.\n", __FUNCTION__
);
807 ike_session_cleanup_idle (void *arg
)
809 ike_session_cleanup((ike_session_t
*)arg
, ike_session_stopped_by_idle
);
813 ike_session_monitor_idle (ike_session_t
*session
)
818 if (session
->traffic_monitor
.dir_idle
== IPSEC_DIR_INBOUND
||
819 session
->traffic_monitor
.dir_idle
== IPSEC_DIR_ANY
) {
820 if (session
->peer_sent_data_sc_idle
) {
821 plog(LLV_DEBUG2
, LOCATION
, NULL
, "%s: restart idle-timeout because peer sent data. monitoring dir %d.\n",
822 __FUNCTION__
, session
->traffic_monitor
.dir_idle
);
823 SCHED_KILL(session
->traffic_monitor
.sc_idle
);
824 if (session
->traffic_monitor
.interv_idle
) {
825 session
->traffic_monitor
.sc_idle
= sched_new(session
->traffic_monitor
.interv_idle
,
826 ike_session_cleanup_idle
,
829 session
->peer_sent_data_sc_idle
= 0;
830 session
->i_sent_data_sc_idle
= 0;
834 if (session
->traffic_monitor
.dir_idle
== IPSEC_DIR_OUTBOUND
||
835 session
->traffic_monitor
.dir_idle
== IPSEC_DIR_ANY
) {
836 if (session
->i_sent_data_sc_idle
) {
837 plog(LLV_DEBUG2
, LOCATION
, NULL
, "%s: restart idle-timeout because i sent data. monitoring dir %d.\n",
838 __FUNCTION__
, session
->traffic_monitor
.dir_idle
);
839 SCHED_KILL(session
->traffic_monitor
.sc_idle
);
840 if (session
->traffic_monitor
.interv_idle
) {
841 session
->traffic_monitor
.sc_idle
= sched_new(session
->traffic_monitor
.interv_idle
,
842 ike_session_cleanup_idle
,
845 session
->peer_sent_data_sc_idle
= 0;
846 session
->i_sent_data_sc_idle
= 0;
853 ike_session_start_traffic_mon (ike_session_t
*session
)
855 if (session
->traffic_monitor
.interv_mon
) {
856 session
->traffic_monitor
.sc_mon
= sched_new(session
->traffic_monitor
.interv_mon
,
857 ike_session_traffic_cop
,
860 if (session
->traffic_monitor
.interv_idle
) {
861 session
->traffic_monitor
.sc_idle
= sched_new(session
->traffic_monitor
.interv_idle
,
862 ike_session_cleanup_idle
,
868 ike_session_ph2_established (struct ph2handle
*iph2
)
870 if (!iph2
->parent_session
) {
871 plog(LLV_DEBUG2
, LOCATION
, NULL
, "invalid parameters in %s.\n", __FUNCTION__
);
874 SCHED_KILL(iph2
->parent_session
->sc_xauth
);
875 if (!iph2
->parent_session
->established
) {
876 gettimeofday(&iph2
->parent_session
->estab_timestamp
, NULL
);
877 iph2
->parent_session
->established
= 1;
878 IPSECSESSIONTRACERESTABLISHED(iph2
->parent_session
);
879 ike_session_start_traffic_mon(iph2
->parent_session
);
880 } else if (iph2
->parent_session
->is_asserted
) {
881 ike_session_start_traffic_mon(iph2
->parent_session
);
883 iph2
->parent_session
->is_asserted
= 0;
884 // nothing happening to this session
885 iph2
->parent_session
->term_reason
= NULL
;
887 ike_session_update_mode(iph2
);
889 #ifdef ENABLE_VPNCONTROL_PORT
890 vpncontrol_notify_peer_resp_ph2(1, iph2
);
891 #endif /* ENABLE_VPNCONTROL_PORT */
892 plog(LLV_DEBUG2
, LOCATION
, NULL
, "%s: ph2 established, spid %d\n", __FUNCTION__
, iph2
->spid
);
896 ike_session_cleanup_ph1 (struct ph1handle
*iph1
)
898 if (iph1
->status
== PHASE1ST_EXPIRED
) {
899 // since this got here via ike_session_cleanup_other_established_ph1s, assumes LIST_FIRST(&iph1->ph2tree) == NULL
900 iph1
->sce
= sched_new(1, isakmp_ph1delete_stub
, iph1
);
904 /* send delete information */
905 if (iph1
->status
== PHASE1ST_ESTABLISHED
) {
906 isakmp_info_send_d1(iph1
);
909 isakmp_ph1expire(iph1
);
913 ike_session_cleanup_ph1_stub (void *p
)
916 ike_session_cleanup_ph1((struct ph1handle
*)p
);
920 ike_session_cleanup_other_established_ph1s (ike_session_t
*session
,
921 struct ph1handle
*new_iph1
)
923 struct ph1handle
*p
, *next
;
924 char *local
, *remote
;
926 if (!session
|| !new_iph1
|| session
!= new_iph1
->parent_session
) {
927 plog(LLV_DEBUG2
, LOCATION
, NULL
, "invalid parameters in %s.\n", __FUNCTION__
);
932 * if we are responder, then we should wait until the server sends a delete notification.
934 if (session
->is_client
&& new_iph1
->side
== RESPONDER
) {
938 for (p
= LIST_FIRST(&session
->ikev1_state
.ph1tree
); p
; p
= next
) {
939 // take next pointer now, since delete change the underlying ph1tree list
940 next
= LIST_NEXT(p
, ph1ofsession_chain
);
942 * TODO: currently, most recently established SA wins. Need to revisit to see if
943 * alternative selections is better (e.g. largest p->index stays).
945 if (p
!= new_iph1
&& !p
->is_dying
) {
947 SCHED_KILL(p
->sce_rekey
);
951 local
= racoon_strdup(saddr2str(p
->local
));
952 remote
= racoon_strdup(saddr2str(p
->remote
));
954 STRDUP_FATAL(remote
);
955 plog(LLV_DEBUG
, LOCATION
, NULL
,
956 "ISAKMP-SA needs to be deleted %s-%s spi:%s\n",
957 local
, remote
, isakmp_pindex(&p
->index
, 0));
961 // first rebind the children ph2s of this dying ph1 to the new ph1.
962 ike_session_rebindph12_from_old_ph1_to_new_ph1 (p
, new_iph1
);
964 if (p
->side
== INITIATOR
) {
965 /* everyone deletes old outbound SA */
966 p
->sce
= sched_new(5, ike_session_cleanup_ph1_stub
, p
);
968 /* responder sets up timer to delete old inbound SAs... say 7 secs later and flags them as rekeyed */
969 p
->sce
= sched_new(7, ike_session_cleanup_ph1_stub
, p
);
976 ike_session_cleanup_ph2 (struct ph2handle
*iph2
)
978 if (iph2
->status
== PHASE2ST_EXPIRED
) {
982 SCHED_KILL(iph2
->sce
);
984 plog(LLV_ERROR
, LOCATION
, NULL
,
985 "about to cleanup ph2: status %d, seq %d dying %d\n",
986 iph2
->status
, iph2
->seq
, iph2
->is_dying
);
988 /* send delete information */
989 if (iph2
->status
== PHASE2ST_ESTABLISHED
) {
990 isakmp_info_send_d2(iph2
);
992 // delete outgoing SAs
993 if (iph2
->approval
) {
996 for (pr
= iph2
->approval
->head
; pr
!= NULL
; pr
= pr
->next
) {
998 pfkey_send_delete(lcconf
->sock_pfkey
,
999 ipsecdoi2pfkey_proto(pr
->proto_id
),
1001 iph2
->src
, iph2
->dst
, pr
->spi_p
/* pr->reqid_out */);
1014 ike_session_cleanup_ph2_stub (void *p
)
1017 ike_session_cleanup_ph2((struct ph2handle
*)p
);
1021 ike_session_cleanup_other_established_ph2s (ike_session_t
*session
,
1022 struct ph2handle
*new_iph2
)
1024 struct ph2handle
*p
, *next
;
1026 if (!session
|| !new_iph2
|| session
!= new_iph2
->parent_session
) {
1027 plog(LLV_DEBUG2
, LOCATION
, NULL
, "invalid parameters in %s.\n", __FUNCTION__
);
1032 * if we are responder, then we should wait until the server sends a delete notification.
1034 if (session
->is_client
&& new_iph2
->side
== RESPONDER
) {
1038 for (p
= LIST_FIRST(&session
->ikev1_state
.ph2tree
); p
; p
= next
) {
1039 // take next pointer now, since delete change the underlying ph2tree list
1040 next
= LIST_NEXT(p
, ph2ofsession_chain
);
1042 * TODO: currently, most recently established SA wins. Need to revisit to see if
1043 * alternative selections is better.
1045 if (p
!= new_iph2
&& p
->spid
== new_iph2
->spid
&& !p
->is_dying
) {
1050 plog(LLV_DEBUG
, LOCATION
, NULL
,
1051 "IPsec-SA needs to be deleted: %s\n",
1052 sadbsecas2str(p
->src
, p
->dst
,
1053 p
->satype
, p
->spid
, 0));
1055 if (p
->side
== INITIATOR
) {
1056 /* responder sets up timer to delete old inbound SAs... say 5 secs later and flags them as rekeyed */
1057 p
->sce
= sched_new(3, ike_session_cleanup_ph2_stub
, p
);
1059 /* responder sets up timer to delete old inbound SAs... say 5 secs later and flags them as rekeyed */
1060 p
->sce
= sched_new(5, ike_session_cleanup_ph2_stub
, p
);
1067 ike_session_stopped_by_controller (ike_session_t
*session
,
1071 plog(LLV_DEBUG2
, LOCATION
, NULL
, "invalid parameters in %s.\n", __FUNCTION__
);
1074 if (session
->stop_timestamp
.tv_sec
||
1075 session
->stop_timestamp
.tv_usec
) {
1076 plog(LLV_DEBUG2
, LOCATION
, NULL
, "already stopped %s.\n", __FUNCTION__
);
1079 session
->stopped_by_vpn_controller
= 1;
1080 gettimeofday(&session
->stop_timestamp
, NULL
);
1081 if (!session
->term_reason
) {
1082 session
->term_reason
= reason
;
1087 ike_sessions_stopped_by_controller (struct sockaddr
*remote
,
1091 ike_session_t
*p
= NULL
;
1094 plog(LLV_DEBUG2
, LOCATION
, NULL
, "invalid parameters in %s.\n", __FUNCTION__
);
1098 for (p
= LIST_FIRST(&ike_session_tree
); p
; p
= LIST_NEXT(p
, chain
)) {
1099 if (withport
&& cmpsaddrstrict(&p
->session_id
.remote
, remote
) == 0 ||
1100 !withport
&& cmpsaddrwop(&p
->session_id
.remote
, remote
) == 0) {
1101 ike_session_stopped_by_controller(p
, reason
);
1107 ike_session_purge_ph2s_by_ph1 (struct ph1handle
*iph1
)
1109 struct ph2handle
*p
, *next
;
1111 if (!iph1
|| !iph1
->parent_session
) {
1112 plog(LLV_DEBUG2
, LOCATION
, NULL
, "invalid parameters in %s.\n", __FUNCTION__
);
1116 for (p
= LIST_FIRST(&iph1
->parent_session
->ikev1_state
.ph2tree
); p
; p
= next
) {
1117 // take next pointer now, since delete change the underlying ph2tree list
1118 next
= LIST_NEXT(p
, ph2ofsession_chain
);
1126 plog(LLV_DEBUG
, LOCATION
, NULL
,
1127 "IPsec-SA needs to be purged: %s\n",
1128 sadbsecas2str(p
->src
, p
->dst
,
1129 p
->satype
, p
->spid
, 0));
1131 ike_session_cleanup_ph2(p
);
1136 ike_session_update_ph2_ports (struct ph2handle
*iph2
)
1138 struct sockaddr
*local
;
1139 struct sockaddr
*remote
;
1141 if (iph2
->parent_session
) {
1142 local
= (struct sockaddr
*)&iph2
->parent_session
->session_id
.local
;
1143 remote
= (struct sockaddr
*)&iph2
->parent_session
->session_id
.remote
;
1145 set_port(iph2
->src
, extract_port(local
));
1146 set_port(iph2
->dst
, extract_port(remote
));
1148 plog(LLV_DEBUG2
, LOCATION
, NULL
, "invalid parent session in %s.\n", __FUNCTION__
);
1153 ike_session_get_sas_for_stats (ike_session_t
*session
,
1156 struct sastat
*stats
,
1157 u_int32_t max_stats
)
1160 struct ph2handle
*iph2
;
1162 if (!session
|| !seq
|| !stats
|| !max_stats
|| (dir
!= IPSEC_DIR_INBOUND
&& dir
!= IPSEC_DIR_OUTBOUND
)) {
1163 plog(LLV_DEBUG2
, LOCATION
, NULL
, "invalid args in %s.\n", __FUNCTION__
);
1168 for (iph2
= LIST_FIRST(&session
->ikev1_state
.ph2tree
); iph2
; iph2
= LIST_NEXT(iph2
, ph2ofsession_chain
)) {
1170 if (iph2
->approval
) {
1173 for (pr
= iph2
->approval
->head
; pr
!= NULL
; pr
= pr
->next
) {
1174 if (pr
->ok
&& pr
->proto_id
== IPSECDOI_PROTO_IPSEC_ESP
) {
1178 if (dir
== IPSEC_DIR_INBOUND
) {
1179 stats
[found
].spi
= pr
->spi
;
1181 stats
[found
].spi
= pr
->spi_p
;
1183 if (++found
== max_stats
) {
1194 ike_session_update_traffic_idle_status (ike_session_t
*session
,
1196 struct sastat
*new_stats
,
1197 u_int32_t max_stats
)
1199 int i
, j
, found
= 0, idle
= 1;
1201 if (!session
|| !new_stats
|| (dir
!= IPSEC_DIR_INBOUND
&& dir
!= IPSEC_DIR_OUTBOUND
)) {
1202 plog(LLV_DEBUG2
, LOCATION
, NULL
, "invalid args in %s.\n", __FUNCTION__
);
1206 if (!session
->established
|| session
->stopped_by_vpn_controller
|| session
->stop_timestamp
.tv_sec
|| session
->stop_timestamp
.tv_usec
) {
1207 plog(LLV_DEBUG2
, LOCATION
, NULL
, "dropping update on invalid session in %s.\n", __FUNCTION__
);
1211 for (i
= 0; i
< max_stats
; i
++) {
1212 if (dir
== IPSEC_DIR_INBOUND
) {
1213 for (j
= 0; j
< session
->traffic_monitor
.num_in_last_poll
; j
++) {
1214 if (new_stats
[i
].spi
!= session
->traffic_monitor
.in_last_poll
[j
].spi
) {
1218 if (new_stats
[i
].lft_c
.sadb_lifetime_bytes
!= session
->traffic_monitor
.in_last_poll
[j
].lft_c
.sadb_lifetime_bytes
) {
1223 for (j
= 0; j
< session
->traffic_monitor
.num_out_last_poll
; j
++) {
1224 if (new_stats
[i
].spi
!= session
->traffic_monitor
.out_last_poll
[j
].spi
) {
1228 if (new_stats
[i
].lft_c
.sadb_lifetime_bytes
!= session
->traffic_monitor
.out_last_poll
[j
].lft_c
.sadb_lifetime_bytes
) {
1233 // new SA.... check for any activity
1235 if (new_stats
[i
].lft_c
.sadb_lifetime_bytes
) {
1236 plog(LLV_DEBUG
, LOCATION
, NULL
, "new SA: dir %d....\n", dir
);
1241 if (dir
== IPSEC_DIR_INBOUND
) {
1242 // overwrite old stats
1243 bzero(session
->traffic_monitor
.in_last_poll
, sizeof(session
->traffic_monitor
.in_last_poll
));
1244 bcopy(new_stats
, session
->traffic_monitor
.in_last_poll
, (max_stats
* sizeof(*new_stats
)));
1245 session
->traffic_monitor
.num_in_last_poll
= max_stats
;
1247 plog(LLV_DEBUG
, LOCATION
, NULL
, "peer sent data....\n");
1248 session
->peer_sent_data_sc_dpd
= 1;
1249 session
->peer_sent_data_sc_idle
= 1;
1252 // overwrite old stats
1253 bzero(session
->traffic_monitor
.out_last_poll
, sizeof(session
->traffic_monitor
.out_last_poll
));
1254 bcopy(new_stats
, session
->traffic_monitor
.out_last_poll
, (max_stats
* sizeof(*new_stats
)));
1255 session
->traffic_monitor
.num_out_last_poll
= max_stats
;
1257 plog(LLV_DEBUG
, LOCATION
, NULL
, "i sent data....\n");
1258 session
->i_sent_data_sc_dpd
= 1;
1259 session
->i_sent_data_sc_idle
= 1;
1263 session
->last_time_data_sc_detected
= time(NULL
);
1265 ike_session_monitor_idle(session
);
1269 ike_session_cleanup (ike_session_t
*session
,
1272 struct ph2handle
*iph2
;
1273 struct ph1handle
*iph1
;
1278 session
->is_dying
= 1;
1280 SCHED_KILL(session
->traffic_monitor
.sc_idle
);
1281 // do ph2's first... we need the ph1s for notifications
1282 for (iph2
= LIST_FIRST(&session
->ikev1_state
.ph2tree
); iph2
; iph2
= LIST_NEXT(iph2
, ph2ofsession_chain
)) {
1283 if (iph2
->status
== PHASE2ST_ESTABLISHED
) {
1284 isakmp_info_send_d2(iph2
);
1286 isakmp_ph2expire(iph2
); // iph2 will go down 1 second later.
1287 ike_session_stopped_by_controller(session
, reason
);
1290 // do the ph1s last.
1291 for (iph1
= LIST_FIRST(&session
->ikev1_state
.ph1tree
); iph1
; iph1
= LIST_NEXT(iph1
, ph1ofsession_chain
)) {
1292 if (iph1
->status
== PHASE1ST_ESTABLISHED
) {
1293 isakmp_info_send_d1(iph1
);
1295 isakmp_ph1expire(iph1
);
1298 // send ipsecManager a notification
1299 if (session
->is_cisco_ipsec
&& reason
&& reason
!= ike_session_stopped_by_vpn_disconnect
) {
1301 if (((struct sockaddr
*)&session
->session_id
.remote
)->sa_family
== AF_INET
) {
1302 address
= ((struct sockaddr_in
*)&session
->session_id
.remote
)->sin_addr
.s_addr
;
1307 if (reason
== ike_session_stopped_by_idle
) {
1308 (void)vpncontrol_notify_ike_failed(VPNCTL_NTYPE_IDLE_TIMEOUT
, FROM_LOCAL
, address
, 0, NULL
);
1310 (void)vpncontrol_notify_ike_failed(VPNCTL_NTYPE_INTERNAL_ERROR
, FROM_LOCAL
, address
, 0, NULL
);
1316 ike_session_has_negoing_ph1 (ike_session_t
*session
)
1318 struct ph1handle
*p
;
1321 plog(LLV_DEBUG2
, LOCATION
, NULL
, "invalid parameters in %s.\n", __FUNCTION__
);
1325 for (p
= LIST_FIRST(&session
->ikev1_state
.ph1tree
); p
; p
= LIST_NEXT(p
, ph1ofsession_chain
)) {
1326 if (!p
->is_dying
&& p
->status
>= PHASE1ST_START
&& p
->status
<= PHASE1ST_ESTABLISHED
) {
1335 ike_session_has_established_ph1 (ike_session_t
*session
)
1337 struct ph1handle
*p
;
1340 plog(LLV_DEBUG2
, LOCATION
, NULL
, "invalid parameters in %s.\n", __FUNCTION__
);
1344 for (p
= LIST_FIRST(&session
->ikev1_state
.ph1tree
); p
; p
= LIST_NEXT(p
, ph1ofsession_chain
)) {
1345 if (!p
->is_dying
&& p
->status
== PHASE1ST_ESTABLISHED
) {
1354 ike_session_has_negoing_ph2 (ike_session_t
*session
)
1356 struct ph2handle
*p
;
1359 plog(LLV_DEBUG2
, LOCATION
, NULL
, "invalid parameters in %s.\n", __FUNCTION__
);
1363 for (p
= LIST_FIRST(&session
->ikev1_state
.ph2tree
); p
; p
= LIST_NEXT(p
, ph2ofsession_chain
)) {
1364 if (!p
->is_dying
&& p
->status
>= PHASE2ST_START
&& p
->status
<= PHASE2ST_ESTABLISHED
) {
1373 ike_session_has_established_ph2 (ike_session_t
*session
)
1375 struct ph2handle
*p
;
1378 plog(LLV_DEBUG2
, LOCATION
, NULL
, "invalid parameters in %s.\n", __FUNCTION__
);
1382 for (p
= LIST_FIRST(&session
->ikev1_state
.ph2tree
); p
; p
= LIST_NEXT(p
, ph2ofsession_chain
)) {
1383 if (!p
->is_dying
&& p
->status
== PHASE2ST_ESTABLISHED
) {
1392 ike_session_cleanup_ph1s_by_ph2 (struct ph2handle
*iph2
)
1394 struct ph1handle
*iph1
;
1396 if (!iph2
|| !iph2
->parent_session
) {
1397 plog(LLV_DEBUG2
, LOCATION
, NULL
, "invalid parameters in %s.\n", __FUNCTION__
);
1401 // phase1 is no longer useful
1402 for (iph1
= LIST_FIRST(&iph2
->parent_session
->ikev1_state
.ph1tree
); iph1
; iph1
= LIST_NEXT(iph1
, ph1ofsession_chain
)) {
1403 if (iph1
->status
== PHASE1ST_ESTABLISHED
) {
1404 isakmp_info_send_d1(iph1
);
1406 isakmp_ph1expire(iph1
);
1411 ike_session_is_client_ph2_rekey (struct ph2handle
*iph2
)
1413 if (iph2
->parent_session
&&
1414 iph2
->parent_session
->is_client
&&
1416 iph2
->parent_session
->is_cisco_ipsec
) {
1423 ike_session_is_client_ph1_rekey (struct ph1handle
*iph1
)
1425 if (iph1
->parent_session
&&
1426 iph1
->parent_session
->is_client
&&
1428 iph1
->parent_session
->is_cisco_ipsec
) {
1435 ike_session_start_xauth_timer (struct ph1handle
*iph1
)
1437 // if there are no more established ph2s, start a timer to teardown the session
1438 if (iph1
->parent_session
&&
1439 iph1
->parent_session
->is_client
&&
1440 iph1
->parent_session
->is_cisco_ipsec
&&
1441 !iph1
->parent_session
->sc_xauth
) {
1442 iph1
->parent_session
->sc_xauth
= sched_new(300 /* 5 mins */,
1443 ike_session_cleanup_xauth_timeout
,
1444 iph1
->parent_session
);
1449 ike_session_stop_xauth_timer (struct ph1handle
*iph1
)
1451 if (iph1
->parent_session
) {
1452 SCHED_KILL(iph1
->parent_session
->sc_xauth
);
1457 ike_session_is_id_ipany (vchar_t
*ext_id
)
1460 u_int8_t type
; /* ID Type */
1461 u_int8_t proto_id
; /* Protocol ID */
1462 u_int16_t port
; /* Port */
1463 u_int32_t addr
; /* IPv4 address */
1467 /* ignore protocol and port */
1468 id_ptr
= (struct id
*)ext_id
->v
;
1469 if (id_ptr
->type
== IPSECDOI_ID_IPV4_ADDR
&&
1470 id_ptr
->addr
== 0) {
1472 } else if (id_ptr
->type
== IPSECDOI_ID_IPV4_ADDR_SUBNET
&&
1473 id_ptr
->mask
== 0 &&
1474 id_ptr
->addr
== 0) {
1477 plog(LLV_DEBUG2
, LOCATION
, NULL
, "not ipany_ids in %s: type %d, addr %x, mask %x.\n",
1478 __FUNCTION__
, id_ptr
->type
, id_ptr
->addr
, id_ptr
->mask
);
1483 ike_session_is_id_portany (vchar_t
*ext_id
)
1486 u_int8_t type
; /* ID Type */
1487 u_int8_t proto_id
; /* Protocol ID */
1488 u_int16_t port
; /* Port */
1489 u_int32_t addr
; /* IPv4 address */
1494 id_ptr
= (struct id
*)ext_id
->v
;
1495 if (id_ptr
->type
== IPSECDOI_ID_IPV4_ADDR
&&
1496 id_ptr
->port
== 0) {
1499 plog(LLV_DEBUG2
, LOCATION
, NULL
, "not portany_ids in %s: type %d, port %x.\n",
1500 __FUNCTION__
, id_ptr
->type
, id_ptr
->port
);
1505 ike_session_set_id_portany (vchar_t
*ext_id
)
1508 u_int8_t type
; /* ID Type */
1509 u_int8_t proto_id
; /* Protocol ID */
1510 u_int16_t port
; /* Port */
1511 u_int32_t addr
; /* IPv4 address */
1516 id_ptr
= (struct id
*)ext_id
->v
;
1517 if (id_ptr
->type
== IPSECDOI_ID_IPV4_ADDR
) {
1524 ike_session_cmp_ph2_ids_ipany (vchar_t
*ext_id
,
1527 if (ike_session_is_id_ipany(ext_id
) &&
1528 ike_session_is_id_ipany(ext_id_p
)) {
1535 * ipsec rekeys for l2tp-over-ipsec fail particularly when client is behind nat because the client's configs and policies don't
1536 * match the server's view of the client's address and port.
1537 * servers behave differently when using this address-port info to generate ids during phase2 rekeys, so try to match the incoming id to
1538 * a variety of info saved in the older phase2.
1541 ike_session_cmp_ph2_ids (struct ph2handle
*iph2
,
1542 struct ph2handle
*older_ph2
)
1544 vchar_t
*portany_id
= NULL
;
1545 vchar_t
*portany_id_p
= NULL
;
1547 if (iph2
->id
&& older_ph2
->id
&&
1548 iph2
->id
->l
== older_ph2
->id
->l
&&
1549 memcmp(iph2
->id
->v
, older_ph2
->id
->v
, iph2
->id
->l
) == 0 &&
1550 iph2
->id_p
&& older_ph2
->id_p
&&
1551 iph2
->id_p
->l
== older_ph2
->id_p
->l
&&
1552 memcmp(iph2
->id_p
->v
, older_ph2
->id_p
->v
, iph2
->id_p
->l
) == 0) {
1555 if (iph2
->ext_nat_id
&& older_ph2
->ext_nat_id
&&
1556 iph2
->ext_nat_id
->l
== older_ph2
->ext_nat_id
->l
&&
1557 memcmp(iph2
->ext_nat_id
->v
, older_ph2
->ext_nat_id
->v
, iph2
->ext_nat_id
->l
) == 0 &&
1558 iph2
->ext_nat_id_p
&& older_ph2
->ext_nat_id_p
&&
1559 iph2
->ext_nat_id_p
->l
== older_ph2
->ext_nat_id_p
->l
&&
1560 memcmp(iph2
->ext_nat_id_p
->v
, older_ph2
->ext_nat_id_p
->v
, iph2
->ext_nat_id_p
->l
) == 0) {
1563 if (iph2
->id
&& older_ph2
->ext_nat_id
&&
1564 iph2
->id
->l
== older_ph2
->ext_nat_id
->l
&&
1565 memcmp(iph2
->id
->v
, older_ph2
->ext_nat_id
->v
, iph2
->id
->l
) == 0 &&
1566 iph2
->id_p
&& older_ph2
->ext_nat_id_p
&&
1567 iph2
->id_p
->l
== older_ph2
->ext_nat_id_p
->l
&&
1568 memcmp(iph2
->id_p
->v
, older_ph2
->ext_nat_id_p
->v
, iph2
->id_p
->l
) == 0) {
1571 if (iph2
->id
&& older_ph2
->ext_nat_id
&&
1572 iph2
->id
->l
== older_ph2
->ext_nat_id
->l
&&
1573 memcmp(iph2
->id
->v
, older_ph2
->ext_nat_id
->v
, iph2
->id
->l
) == 0 &&
1574 iph2
->id_p
&& older_ph2
->id_p
&&
1575 iph2
->id_p
->l
== older_ph2
->id_p
->l
&&
1576 memcmp(iph2
->id_p
->v
, older_ph2
->id_p
->v
, iph2
->id_p
->l
) == 0) {
1579 if (iph2
->id
&& older_ph2
->id
&&
1580 iph2
->id
->l
== older_ph2
->id
->l
&&
1581 memcmp(iph2
->id
->v
, older_ph2
->id
->v
, iph2
->id
->l
) == 0 &&
1582 iph2
->id_p
&& older_ph2
->ext_nat_id_p
&&
1583 iph2
->id_p
->l
== older_ph2
->ext_nat_id_p
->l
&&
1584 memcmp(iph2
->id_p
->v
, older_ph2
->ext_nat_id_p
->v
, iph2
->id_p
->l
) == 0) {
1588 /* check if the external id has a wildcard port and compare ids accordingly */
1589 if ((older_ph2
->ext_nat_id
&& ike_session_is_id_portany(older_ph2
->ext_nat_id
)) ||
1590 (older_ph2
->ext_nat_id_p
&& ike_session_is_id_portany(older_ph2
->ext_nat_id_p
))) {
1591 // try ignoring ports in iph2->id and iph2->id
1592 if (iph2
->id
&& (portany_id
= vdup(iph2
->id
))) {
1593 ike_session_set_id_portany(portany_id
);
1595 if (iph2
->id_p
&& (portany_id_p
= vdup(iph2
->id_p
))) {
1596 ike_session_set_id_portany(portany_id_p
);
1598 if (portany_id
&& older_ph2
->ext_nat_id
&&
1599 portany_id
->l
== older_ph2
->ext_nat_id
->l
&&
1600 memcmp(portany_id
->v
, older_ph2
->ext_nat_id
->v
, portany_id
->l
) == 0 &&
1601 portany_id_p
&& older_ph2
->ext_nat_id_p
&&
1602 portany_id_p
->l
== older_ph2
->ext_nat_id_p
->l
&&
1603 memcmp(portany_id_p
->v
, older_ph2
->ext_nat_id_p
->v
, portany_id_p
->l
) == 0) {
1608 vfree(portany_id_p
);
1612 if (portany_id
&& iph2
->id
&& older_ph2
->ext_nat_id
&&
1613 iph2
->id
->l
== older_ph2
->ext_nat_id
->l
&&
1614 memcmp(portany_id
->v
, older_ph2
->ext_nat_id
->v
, portany_id
->l
) == 0 &&
1615 iph2
->id_p
&& older_ph2
->id_p
&&
1616 iph2
->id_p
->l
== older_ph2
->id_p
->l
&&
1617 memcmp(iph2
->id_p
->v
, older_ph2
->id_p
->v
, iph2
->id_p
->l
) == 0) {
1622 vfree(portany_id_p
);
1626 if (portany_id_p
&& iph2
->id
&& older_ph2
->id
&&
1627 iph2
->id
->l
== older_ph2
->id
->l
&&
1628 memcmp(iph2
->id
->v
, older_ph2
->id
->v
, iph2
->id
->l
) == 0 &&
1629 iph2
->id_p
&& older_ph2
->ext_nat_id_p
&&
1630 iph2
->id_p
->l
== older_ph2
->ext_nat_id_p
->l
&&
1631 memcmp(portany_id_p
->v
, older_ph2
->ext_nat_id_p
->v
, portany_id_p
->l
) == 0) {
1636 vfree(portany_id_p
);
1644 vfree(portany_id_p
);
1651 ike_session_get_sainfo_r (struct ph2handle
*iph2
)
1653 if (iph2
->parent_session
&&
1654 iph2
->parent_session
->is_client
&&
1655 iph2
->id
&& iph2
->id_p
) {
1656 struct ph2handle
*p
;
1657 int ipany_ids
= ike_session_cmp_ph2_ids_ipany(iph2
->id
, iph2
->id_p
);
1658 plog(LLV_DEBUG2
, LOCATION
, NULL
, "ipany_ids %d in %s.\n", ipany_ids
, __FUNCTION__
);
1660 for (p
= LIST_FIRST(&iph2
->parent_session
->ikev1_state
.ph2tree
); p
; p
= LIST_NEXT(p
, ph2ofsession_chain
)) {
1661 if (iph2
!= p
&& !p
->is_dying
&& p
->status
>= PHASE2ST_ESTABLISHED
&&
1662 p
->sainfo
&& !p
->sainfo
->to_delete
&& !p
->sainfo
->to_remove
) {
1663 plog(LLV_DEBUG2
, LOCATION
, NULL
, "candidate ph2 found in %s.\n", __FUNCTION__
);
1665 ike_session_cmp_ph2_ids(iph2
, p
) == 0) {
1666 plog(LLV_DEBUG2
, LOCATION
, NULL
, "candidate ph2 matched in %s.\n", __FUNCTION__
);
1667 iph2
->sainfo
= p
->sainfo
;
1669 iph2
->spid
= p
->spid
;
1671 plog(LLV_DEBUG2
, LOCATION
, NULL
, "%s: pre-assigned spid %d.\n", __FUNCTION__
, iph2
->spid
);
1673 if (p
->ext_nat_id
) {
1674 if (iph2
->ext_nat_id
) {
1675 vfree(iph2
->ext_nat_id
);
1677 iph2
->ext_nat_id
= vdup(p
->ext_nat_id
);
1679 if (p
->ext_nat_id_p
) {
1680 if (iph2
->ext_nat_id_p
) {
1681 vfree(iph2
->ext_nat_id_p
);
1683 iph2
->ext_nat_id_p
= vdup(p
->ext_nat_id_p
);
1694 ike_session_get_proposal_r (struct ph2handle
*iph2
)
1696 if (iph2
->parent_session
&&
1697 iph2
->parent_session
->is_client
&&
1698 iph2
->id
&& iph2
->id_p
) {
1699 struct ph2handle
*p
;
1700 int ipany_ids
= ike_session_cmp_ph2_ids_ipany(iph2
->id
, iph2
->id_p
);
1701 plog(LLV_DEBUG2
, LOCATION
, NULL
, "ipany_ids %d in %s.\n", ipany_ids
, __FUNCTION__
);
1703 for (p
= LIST_FIRST(&iph2
->parent_session
->ikev1_state
.ph2tree
); p
; p
= LIST_NEXT(p
, ph2ofsession_chain
)) {
1704 if (iph2
!= p
&& !p
->is_dying
&& p
->status
>= PHASE2ST_ESTABLISHED
&&
1706 plog(LLV_DEBUG2
, LOCATION
, NULL
, "candidate ph2 found in %s.\n", __FUNCTION__
);
1708 ike_session_cmp_ph2_ids(iph2
, p
) == 0) {
1709 plog(LLV_DEBUG2
, LOCATION
, NULL
, "candidate ph2 matched in %s.\n", __FUNCTION__
);
1710 iph2
->proposal
= dupsaprop(p
->approval
, 1);
1712 iph2
->spid
= p
->spid
;
1714 plog(LLV_DEBUG2
, LOCATION
, NULL
, "%s: pre-assigned spid %d.\n", __FUNCTION__
, iph2
->spid
);
1725 ike_session_update_natt_version (struct ph1handle
*iph1
)
1727 if (iph1
->parent_session
) {
1728 if (iph1
->natt_options
) {
1729 iph1
->parent_session
->natt_version
= iph1
->natt_options
->version
;
1731 iph1
->parent_session
->natt_version
= 0;
1737 ike_session_get_natt_version (struct ph1handle
*iph1
)
1739 if (iph1
->parent_session
) {
1740 return(iph1
->parent_session
->natt_version
);
1746 ike_session_drop_rekey (ike_session_t
*session
, ike_session_rekey_type_t rekey_type
)
1749 if (session
->is_btmm_ipsec
&&
1750 session
->last_time_data_sc_detected
&&
1751 session
->traffic_monitor
.interv_mon
&&
1752 session
->traffic_monitor
.interv_idle
) {
1753 // for btmm: drop ph1/ph2 rekey if session is idle
1754 time_t now
= time(NULL
);
1756 if ((now
- session
->last_time_data_sc_detected
) > (session
->traffic_monitor
.interv_mon
<< 1)) {
1757 plog(LLV_DEBUG2
, LOCATION
, NULL
, "btmm session is idle: drop ph%drekey.\n",
1761 } else if (!session
->is_btmm_ipsec
) {
1762 if (rekey_type
== IKE_SESSION_REKEY_TYPE_PH1
&&
1763 !ike_session_has_negoing_ph2(session
)) {
1764 // for vpn: only drop ph1 if there are no more ph2s.
1765 plog(LLV_DEBUG2
, LOCATION
, NULL
, "vpn session is idle: drop ph1 rekey.\n");
1774 * this is called after racooon receives a 'kIOMessageSystemHasPoweredOn'
1775 * a lot is done to make sure that we don't sweep a session that's already been asserted.
1776 * however, it'll be too bad if the assertion comes after the session has already been swept.
1779 ike_session_sweep_sleepwake (void)
1783 // flag session as dying if all ph1/ph2 are dead/dying
1784 for (p
= LIST_FIRST(&ike_session_tree
); p
; p
= LIST_NEXT(p
, chain
)) {
1786 plog(LLV_DEBUG2
, LOCATION
, NULL
, "skipping sweep of dying session.\n");
1789 SCHED_KILL(p
->sc_xauth
);
1790 if (p
->is_asserted
) {
1791 // for asserted session, traffic monitors will be restared after phase2 becomes established.
1792 SCHED_KILL(p
->traffic_monitor
.sc_mon
);
1793 SCHED_KILL(p
->traffic_monitor
.sc_idle
);
1794 plog(LLV_DEBUG2
, LOCATION
, NULL
, "skipping sweep of asserted session.\n");
1798 if (!ike_session_has_established_ph1(p
) && !ike_session_has_established_ph2(p
)) {
1800 plog(LLV_DEBUG2
, LOCATION
, NULL
, "session died while sleeping.\n");
1802 if (p
->traffic_monitor
.sc_mon
) {
1803 if (p
->traffic_monitor
.sc_mon
->xtime
<= swept_at
) {
1804 SCHED_KILL(p
->traffic_monitor
.sc_mon
);
1805 if (!p
->is_dying
&& p
->traffic_monitor
.interv_mon
) {
1806 p
->traffic_monitor
.sc_mon
= sched_new(p
->traffic_monitor
.interv_mon
,
1807 ike_session_traffic_cop
,
1812 if (p
->traffic_monitor
.sc_idle
) {
1813 if (p
->traffic_monitor
.sc_idle
->xtime
<= swept_at
) {
1814 SCHED_KILL(p
->traffic_monitor
.sc_idle
);
1815 if (!p
->is_dying
&& p
->traffic_monitor
.interv_idle
) {
1816 p
->traffic_monitor
.sc_idle
= sched_new(p
->traffic_monitor
.interv_idle
,
1817 ike_session_cleanup_idle
,
1826 * this is called after racooon receives an assert command from the controller/pppd.
1827 * this is intended to make racoon prepare to rekey both SAs because a network event occurred.
1828 * in the event of a sleepwake, the assert could happen before or after 'ike_session_sweep_sleepwake'.
1831 ike_session_assert_session (ike_session_t
*session
)
1833 struct ph2handle
*iph2
, *iph2_next
;
1834 struct ph1handle
*iph1
, *iph1_next
;
1836 if (!session
|| session
->is_dying
) {
1837 plog(LLV_DEBUG2
, LOCATION
, NULL
, "invalid parameters in %s.\n", __FUNCTION__
);
1841 // the goal is to prepare the session for fresh rekeys by silently deleting the currently active phase2s
1842 for (iph2
= LIST_FIRST(&session
->ikev1_state
.ph2tree
); iph2
; iph2
= iph2_next
) {
1843 // take next pointer now, since delete change the underlying ph2tree list
1844 iph2_next
= LIST_NEXT(iph2
, ph2ofsession_chain
);
1845 if (!iph2
->is_dying
&& iph2
->status
< PHASE2ST_EXPIRED
) {
1846 SCHED_KILL(iph2
->sce
);
1849 // delete SAs (in the kernel)
1850 if (iph2
->status
== PHASE2ST_ESTABLISHED
&& iph2
->approval
) {
1853 for (pr
= iph2
->approval
->head
; pr
!= NULL
; pr
= pr
->next
) {
1856 plog(LLV_DEBUG
, LOCATION
, NULL
,
1857 "assert: phase2 %s deleted\n",
1858 sadbsecas2str(iph2
->src
, iph2
->dst
, iph2
->satype
, iph2
->spid
, ipsecdoi2pfkey_mode(pr
->encmode
)));
1860 pfkey_send_delete(lcconf
->sock_pfkey
,
1861 ipsecdoi2pfkey_proto(pr
->proto_id
),
1862 ipsecdoi2pfkey_mode(pr
->encmode
),
1863 iph2
->src
, iph2
->dst
, pr
->spi_p
);
1868 iph2
->status
= PHASE2ST_EXPIRED
; // we want to delete SAs without telling the PEER
1869 iph2
->sce
= sched_new(3, ike_session_cleanup_ph2_stub
, iph2
);
1873 // the goal is to prepare the session for fresh rekeys by silently deleting the currently active phase1s
1874 for (iph1
= LIST_FIRST(&session
->ikev1_state
.ph1tree
); iph1
; iph1
= iph1_next
) {
1875 // take next pointer now, since delete change the underlying ph1tree list
1876 iph1_next
= LIST_NEXT(iph1
, ph1ofsession_chain
);
1877 if (!iph1
->is_dying
&& iph1
->status
< PHASE1ST_EXPIRED
) {
1878 SCHED_KILL(iph1
->sce
);
1879 SCHED_KILL(iph1
->sce_rekey
);
1883 plog(LLV_DEBUG
, LOCATION
, NULL
,
1884 "assert: phase1 %s deleted\n",
1885 isakmp_pindex(&iph1
->index
, 0));
1887 ike_session_unbindph12_from_ph1(iph1
);
1889 iph1
->status
= PHASE1ST_EXPIRED
; // we want to delete SAs without telling the PEER
1890 /* responder sets up timer to delete old inbound SAs... say 7 secs later and flags them as rekeyed */
1891 iph1
->sce
= sched_new(5, ike_session_cleanup_ph1_stub
, iph1
);
1894 session
->is_asserted
= 1;
1900 ike_session_assert (struct sockaddr
*local
,
1901 struct sockaddr
*remote
)
1903 ike_session_t
*sess
;
1905 if (!local
|| !remote
) {
1906 plog(LLV_DEBUG2
, LOCATION
, NULL
, "invalid parameters in %s.\n", __FUNCTION__
);
1910 if ((sess
= ike_session_get_session(local
, remote
, FALSE
))) {
1911 return(ike_session_assert_session(sess
));
1917 ike_session_ph2_retransmits (struct ph2handle
*iph2
)
1919 int num_retransmits
;
1921 if (!iph2
->is_dying
&&
1924 iph2
->ph1
->sce_rekey
&& !iph2
->ph1
->sce_rekey
->dead
&&
1925 iph2
->side
== INITIATOR
&&
1926 iph2
->parent_session
&&
1927 !iph2
->parent_session
->is_cisco_ipsec
&& /* not for Cisco */
1928 iph2
->parent_session
->is_client
) {
1929 num_retransmits
= iph2
->ph1
->rmconf
->retry_counter
- iph2
->retry_counter
;
1930 if (num_retransmits
== 3) {
1932 * phase2 negotiation is stalling on retransmits, inspite of a valid ph1.
1933 * one of the following is possible:
1934 * - (0) severe packet loss.
1935 * - (1) the peer is dead.
1936 * - (2) the peer is out of sync hence dropping this phase2 rekey (and perhaps responding with insecure
1937 * invalid-cookie notifications... but those are untrusted and so we can't rekey phase1 off that)
1938 * (2.1) the peer rebooted (or process restarted) and is now alive.
1939 * (2.2) the peer has deleted phase1 without notifying us (or the notification got dropped somehow).
1940 * (2.3) the peer has a policy/bug stopping this phase2 rekey
1942 * in all these cases, one sure way to know is to trigger a phase1 rekey early.
1944 plog(LLV_DEBUG2
, LOCATION
, NULL
, "many phase2 retransmits: try phase1 rekey and this phase2 to quit earlier.\n");
1945 isakmp_ph1rekeyexpire(iph2
->ph1
, TRUE
);
1946 iph2
->retry_counter
= 0;
1952 ike_session_ph1_retransmits (struct ph1handle
*iph1
)
1954 int num_retransmits
;
1956 if (!iph1
->is_dying
&&
1959 iph1
->status
>= PHASE1ST_START
&& iph1
->status
< PHASE1ST_ESTABLISHED
&&
1960 iph1
->side
== INITIATOR
&&
1961 iph1
->parent_session
&&
1962 iph1
->parent_session
->is_client
&&
1963 !ike_session_has_other_negoing_ph1(iph1
->parent_session
, iph1
)) {
1964 num_retransmits
= iph1
->rmconf
->retry_counter
- iph1
->retry_counter
;
1965 if (num_retransmits
== 3) {
1966 plog(LLV_DEBUG2
, LOCATION
, NULL
, "many phase1 retransmits: try quit earlier.\n");
1967 iph1
->retry_counter
= 0;