1 /* $NetBSD: handler.c,v 1.9.6.6 2007/06/06 09:20:12 vanhu Exp $ */
3 /* Id: handler.c,v 1.28 2006/05/26 12:17:29 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
36 #include <sys/types.h>
37 #include <sys/param.h>
38 #include <sys/socket.h>
59 #include "grabmyaddr.h"
60 #include "algorithm.h"
61 #include "crypto_openssl.h"
64 #include "isakmp_var.h"
67 #include "isakmp_xauth.h"
68 #include "isakmp_cfg.h"
70 #include "isakmp_inf.h"
72 #include "remoteconf.h"
73 #include "localconf.h"
76 #include "nattraversal.h"
77 #include "ike_session.h"
78 #include "isakmp_frag.h"
82 #include "power_mgmt.h"
84 extern LIST_HEAD(_ike_session_tree_
, ike_session
) ike_session_tree
;
85 static LIST_HEAD(_ctdtree_
, contacted
) ctdtree
;
86 static LIST_HEAD(_rcptree_
, recvdpkt
) rcptree
;
88 static void ike_session_del_recvdpkt (struct recvdpkt
*);
89 static void ike_session_rem_recvdpkt (struct recvdpkt
*);
90 static void sweep_recvdpkt (void *);
93 * functions about management of the isakmp status table
95 /* %%% management phase 1 handler */
97 * search for isakmpsa handler with isakmp index.
100 extern caddr_t
val2str (const char *, size_t);
102 static phase1_handle_t
*
103 getph1byindex(ike_session_t
*session
, isakmp_index
*index
)
105 phase1_handle_t
*p
= NULL
;
107 LIST_FOREACH(p
, &session
->ph1tree
, ph1ofsession_chain
) {
108 if (FSM_STATE_IS_EXPIRED(p
->status
))
110 if (memcmp(&p
->index
, index
, sizeof(*index
)) == 0)
118 ike_session_getph1byindex(ike_session_t
*session
, isakmp_index
*index
)
121 ike_session_t
*cur_session
= NULL
;
124 return getph1byindex(session
, index
);
126 LIST_FOREACH(cur_session
, &ike_session_tree
, chain
) {
127 if ((p
= getph1byindex(cur_session
, index
)) != NULL
)
135 * search for isakmp handler by i_ck in index.
138 static phase1_handle_t
*
139 getph1byindex0 (ike_session_t
*session
, isakmp_index
*index
)
141 phase1_handle_t
*p
= NULL
;
143 LIST_FOREACH(p
, &session
->ph1tree
, ph1ofsession_chain
) {
144 if (FSM_STATE_IS_EXPIRED(p
->status
))
146 if (memcmp(&p
->index
.i_ck
, &index
->i_ck
, sizeof(cookie_t
)) == 0)
153 ike_session_getph1byindex0(ike_session_t
*session
, isakmp_index
*index
)
155 phase1_handle_t
*p
= NULL
;
156 ike_session_t
*cur_session
= NULL
;
159 return getph1byindex0(session
, index
);
161 LIST_FOREACH(cur_session
, &ike_session_tree
, chain
) {
162 if ((p
= getph1byindex0(cur_session
, index
)) != NULL
)
170 * search for isakmpsa handler by source and remote address.
171 * don't use port number to search because this function search
172 * with phase 2's destinaion.
175 ike_session_getph1byaddr(ike_session_t
*session
, struct sockaddr_storage
*local
, struct sockaddr_storage
*remote
)
177 phase1_handle_t
*p
= NULL
;
179 plog(ASL_LEVEL_DEBUG
, "getph1byaddr: start\n");
180 plog(ASL_LEVEL_DEBUG
, "local: %s\n", saddr2str((struct sockaddr
*)local
));
181 plog(ASL_LEVEL_DEBUG
, "remote: %s\n", saddr2str((struct sockaddr
*)remote
));
183 LIST_FOREACH(p
, &session
->ph1tree
, ph1ofsession_chain
) {
184 if (FSM_STATE_IS_EXPIRED(p
->status
))
186 plog(ASL_LEVEL_DEBUG
, "p->local: %s\n", saddr2str((struct sockaddr
*)p
->local
));
187 plog(ASL_LEVEL_DEBUG
, "p->remote: %s\n", saddr2str((struct sockaddr
*)p
->remote
));
188 if (CMPSADDR(local
, p
->local
) == 0
189 && CMPSADDR(remote
, p
->remote
) == 0){
190 plog(ASL_LEVEL_DEBUG
, "matched\n");
195 plog(ASL_LEVEL_DEBUG
, "no match\n");
200 static phase1_handle_t
*
201 sgetph1byaddrwop(ike_session_t
*session
, struct sockaddr_storage
*local
, struct sockaddr_storage
*remote
)
203 phase1_handle_t
*p
= NULL
;
205 LIST_FOREACH(p
, &session
->ph1tree
, ph1ofsession_chain
) {
206 if (FSM_STATE_IS_EXPIRED(p
->status
))
208 if (cmpsaddrwop(local
, p
->local
) == 0
209 && cmpsaddrwop(remote
, p
->remote
) == 0)
217 ike_session_getph1byaddrwop(ike_session_t
*session
, struct sockaddr_storage
*local
, struct sockaddr_storage
*remote
)
220 ike_session_t
*cur_session
= NULL
;
223 return sgetph1byaddrwop(session
, local
, remote
);
225 LIST_FOREACH(cur_session
, &ike_session_tree
, chain
) {
226 if ((p
= sgetph1byaddrwop(cur_session
, local
, remote
)) != NULL
)
234 * search for isakmpsa handler by remote address.
235 * don't use port number to search because this function search
236 * with phase 2's destinaion.
239 sike_session_getph1bydstaddrwop(ike_session_t
*session
, struct sockaddr_storage
*remote
)
241 phase1_handle_t
*p
= NULL
;
243 LIST_FOREACH(p
, &session
->ph1tree
, ph1ofsession_chain
) {
244 if (FSM_STATE_IS_EXPIRED(p
->status
)) {
247 if (remote
->ss_family
== AF_INET
&&
248 p
->nat64_prefix
.length
) {
249 struct in_addr address
;
250 nw_nat64_extract_v4(&p
->nat64_prefix
, &((struct sockaddr_in6
*)p
->remote
)->sin6_addr
, &address
);
251 if (((struct sockaddr_in
*)remote
)->sin_addr
.s_addr
== address
.s_addr
) {
254 } else if (cmpsaddrwop(remote
, p
->remote
) == 0) {
263 ike_session_getph1bydstaddrwop(ike_session_t
*session
, struct sockaddr_storage
*remote
)
266 ike_session_t
*cur_session
= NULL
;
269 return sike_session_getph1bydstaddrwop(session
, remote
);
271 LIST_FOREACH(cur_session
, &ike_session_tree
, chain
) {
272 if ((p
= sike_session_getph1bydstaddrwop(cur_session
, remote
)) != NULL
)
280 ike_session_islast_ph1(phase1_handle_t
*ph1
)
282 phase1_handle_t
*p
= NULL
;
284 LIST_FOREACH(p
, &ph1
->parent_session
->ph1tree
, ph1ofsession_chain
) {
285 if (p
->is_dying
|| FSM_STATE_IS_EXPIRED(p
->status
))
287 if (CMPSADDR(ph1
->remote
, p
->remote
) == 0) {
297 * create new isakmp Phase 1 status record to handle isakmp in Phase1
300 ike_session_newph1(unsigned int version
)
302 phase1_handle_t
*iph1
;
304 /* create new iph1 */
305 iph1
= racoon_calloc(1, sizeof(*iph1
));
308 iph1
->version
= version
;
311 iph1
->dpd_support
= 0;
312 iph1
->dpd_lastack
= 0;
315 iph1
->peer_sent_ike
= 0;
316 iph1
->dpd_r_u
= NULL
;
318 #ifdef ENABLE_VPNCONTROL_PORT
319 iph1
->ping_sched
= NULL
;
322 plog(ASL_LEVEL_DEBUG
, "*** New Phase 1\n");
327 * delete new isakmp Phase 1 status record to handle isakmp in Phase1
330 ike_session_delph1(phase1_handle_t
*iph1
)
336 if (iph1
->natt_options
) {
337 racoon_free(iph1
->natt_options
);
338 iph1
->natt_options
= NULL
;
344 isakmp_cfg_rmstate(iph1
);
345 VPTRINIT(iph1
->xauth_awaiting_userinput_msg
);
350 SCHED_KILL(iph1
->dpd_r_u
);
352 #ifdef ENABLE_VPNCONTROL_PORT
353 if (iph1
->ping_sched
)
354 SCHED_KILL(iph1
->ping_sched
);
358 racoon_free(iph1
->remote
);
362 racoon_free(iph1
->local
);
366 if (iph1
->approval
) {
367 delisakmpsa(iph1
->approval
);
368 iph1
->approval
= NULL
;
371 sched_scrub_param(iph1
);
373 SCHED_KILL(iph1
->sce
);
375 SCHED_KILL(iph1
->sce_rekey
);
377 SCHED_KILL(iph1
->scr
);
379 VPTRINIT(iph1
->sendbuf
);
381 VPTRINIT(iph1
->dhpriv
);
382 VPTRINIT(iph1
->dhpub
);
383 VPTRINIT(iph1
->dhpub_p
);
384 VPTRINIT(iph1
->dhgxy
);
385 VPTRINIT(iph1
->nonce
);
386 VPTRINIT(iph1
->nonce_p
);
387 VPTRINIT(iph1
->skeyid
);
388 VPTRINIT(iph1
->skeyid_d
);
389 VPTRINIT(iph1
->skeyid_a
);
390 VPTRINIT(iph1
->skeyid_a_p
);
391 VPTRINIT(iph1
->skeyid_e
);
392 VPTRINIT(iph1
->skeyid_e_p
);
394 VPTRINIT(iph1
->key_p
);
395 VPTRINIT(iph1
->hash
);
397 VPTRINIT(iph1
->sig_p
);
398 oakley_delcert(iph1
->cert
);
400 oakley_delcert(iph1
->cert_p
);
402 oakley_delcert(iph1
->crl_p
);
404 oakley_delcert(iph1
->cr_p
);
407 VPTRINIT(iph1
->id_p
);
409 if(iph1
->approval
!= NULL
)
410 delisakmpsa(iph1
->approval
);
413 oakley_delivm(iph1
->ivm
);
418 VPTRINIT(iph1
->sa_ret
);
421 release_rmconf(iph1
->rmconf
);
429 ike_session_flush_all_phase1_for_session(ike_session_t
*session
, int ignore_estab_or_assert_handles
)
431 phase1_handle_t
*p
, *next
;
433 LIST_FOREACH_SAFE(p
, &session
->ph1tree
, ph1ofsession_chain
, next
) {
434 if (ignore_estab_or_assert_handles
&& p
->parent_session
&& !p
->parent_session
->stopped_by_vpn_controller
&& p
->parent_session
->is_asserted
) {
435 plog(ASL_LEVEL_DEBUG
,
436 "Skipping Phase 1 %s that's asserted...\n",
437 isakmp_pindex(&p
->index
, 0));
441 /* send delete information */
442 if (FSM_STATE_IS_ESTABLISHED(p
->status
)) {
443 if (ignore_estab_or_assert_handles
&&
444 (ike_session_has_negoing_ph2(p
->parent_session
) || ike_session_has_established_ph2(p
->parent_session
))) {
445 plog(ASL_LEVEL_DEBUG
,
446 "Skipping Phase 1 %s that's established... because it's needed by children Phase 2s\n",
447 isakmp_pindex(&p
->index
, 0));
450 /* send delete information */
451 plog(ASL_LEVEL_DEBUG
,
452 "Got a Phase 1 %s to flush...\n",
453 isakmp_pindex(&p
->index
, 0));
454 isakmp_info_send_d1(p
);
457 ike_session_stopped_by_controller(p
->parent_session
,
458 ike_session_stopped_by_flush
);
460 ike_session_unlink_phase1(p
);
468 ike_session_flush_all_phase1(int ignore_estab_or_assert_handles
)
470 ike_session_t
*session
= NULL
;
471 ike_session_t
*next_session
= NULL
;
473 plog(ASL_LEVEL_DEBUG
,
474 "Flushing Phase 1 handles: ignore_estab_or_assert %d...\n", ignore_estab_or_assert_handles
);
476 LIST_FOREACH_SAFE(session
, &ike_session_tree
, chain
, next_session
) {
477 ike_session_flush_all_phase1_for_session(session
, ignore_estab_or_assert_handles
);
484 * search ph2handle with policy id.
487 ike_session_getph2byspid(u_int32_t spid
)
489 ike_session_t
*session
= NULL
;
492 LIST_FOREACH(session
, &ike_session_tree
, chain
) {
493 LIST_FOREACH(p
, &session
->ph2tree
, ph2ofsession_chain
) {
495 * there are ph2handle independent on policy
496 * such like informational exchange.
508 * search ph2handle with sequence number.
509 * Used by PF_KEY functions to locate the phase2
512 ike_session_getph2byseq(u_int32_t seq
)
514 ike_session_t
*session
;
517 LIST_FOREACH(session
, &ike_session_tree
, chain
) {
518 LIST_FOREACH(p
, &session
->ph2tree
, ph2ofsession_chain
) {
527 * search ph2handle with message id.
530 ike_session_getph2bymsgid(phase1_handle_t
*iph1
, u_int32_t msgid
)
534 LIST_FOREACH(p
, &iph1
->parent_session
->ph2tree
, ph2ofsession_chain
) {
535 if (p
->msgid
== msgid
&& !p
->is_defunct
)
543 ike_session_getonlyph2(phase1_handle_t
*iph1
)
545 phase2_handle_t
*only_ph2
= NULL
;
546 phase2_handle_t
*p
= NULL
;
548 LIST_FOREACH(p
, &iph1
->bound_ph2tree
, ph2ofsession_chain
) {
549 if (only_ph2
) return NULL
;
557 ike_session_getph2byid(struct sockaddr_storage
*src
, struct sockaddr_storage
*dst
, u_int32_t spid
)
559 ike_session_t
*session
= NULL
;
560 ike_session_t
*next_session
= NULL
;
562 phase2_handle_t
*next_iph2
;
564 LIST_FOREACH_SAFE(session
, &ike_session_tree
, chain
, next_session
) {
565 LIST_FOREACH_SAFE(p
, &session
->ph2tree
, ph2ofsession_chain
, next_iph2
) {
566 if (spid
== p
->spid
&&
567 CMPSADDR(src
, p
->src
) == 0 &&
568 CMPSADDR(dst
, p
->dst
) == 0){
569 /* Sanity check to detect zombie handlers
570 * XXX Sould be done "somewhere" more interesting,
571 * because we have lots of getph2byxxxx(), but this one
572 * is called by pk_recvacquire(), so is the most important.
574 if(!FSM_STATE_IS_ESTABLISHED_OR_EXPIRED(p
->status
) &&
575 p
->retry_counter
== 0
576 && p
->sce
== 0 && p
->scr
== 0 &&
577 p
->retry_checkph1
== 0){
578 plog(ASL_LEVEL_DEBUG
,
579 "Zombie ph2 found, expiring it\n");
592 ike_session_getph2bysaddr(struct sockaddr_storage
*src
, struct sockaddr_storage
*dst
)
594 ike_session_t
*session
;
597 LIST_FOREACH(session
, &ike_session_tree
, chain
) {
598 LIST_FOREACH(p
, &session
->ph2tree
, chain
) {
599 if (cmpsaddrstrict(src
, p
->src
) == 0 &&
600 cmpsaddrstrict(dst
, p
->dst
) == 0)
607 #endif /* NOT_USED */
610 * call by pk_recvexpire().
613 ike_session_getph2bysaidx(struct sockaddr_storage
*src
, struct sockaddr_storage
*dst
, u_int proto_id
, u_int32_t spi
)
615 ike_session_t
*session
;
616 phase2_handle_t
*iph2
;
619 LIST_FOREACH(session
, &ike_session_tree
, chain
) {
620 LIST_FOREACH(iph2
, &session
->ph2tree
, ph2ofsession_chain
) {
621 if (iph2
->proposal
== NULL
&& iph2
->approval
== NULL
)
623 if (iph2
->approval
!= NULL
) {
624 for (pr
= iph2
->approval
->head
; pr
!= NULL
;
626 if (proto_id
!= pr
->proto_id
)
628 if (spi
== pr
->spi
|| spi
== pr
->spi_p
)
631 } else if (iph2
->proposal
!= NULL
) {
632 for (pr
= iph2
->proposal
->head
; pr
!= NULL
;
634 if (proto_id
!= pr
->proto_id
)
647 ike_session_getph2bysaidx2(struct sockaddr_storage
*src
, struct sockaddr_storage
*dst
, u_int proto_id
, u_int32_t spi
, u_int32_t
*opposite_spi
)
649 ike_session_t
*session
;
650 phase2_handle_t
*iph2
;
653 LIST_FOREACH(session
, &ike_session_tree
, chain
) {
654 LIST_FOREACH(iph2
, &session
->ph2tree
, ph2ofsession_chain
) {
655 if (iph2
->proposal
== NULL
&& iph2
->approval
== NULL
)
657 if (iph2
->approval
!= NULL
) {
658 for (pr
= iph2
->approval
->head
; pr
!= NULL
;
660 if (proto_id
!= pr
->proto_id
)
662 if (spi
== pr
->spi
|| spi
== pr
->spi_p
) {
664 *opposite_spi
= (spi
== pr
->spi
)? pr
->spi_p
: pr
->spi
;
669 } else if (iph2
->proposal
!= NULL
) {
670 for (pr
= iph2
->proposal
->head
; pr
!= NULL
;
672 if (proto_id
!= pr
->proto_id
)
674 if (spi
== pr
->spi
|| spi
== pr
->spi_p
) {
676 *opposite_spi
= (spi
== pr
->spi
)? pr
->spi_p
: pr
->spi
;
689 * create new isakmp Phase 2 status record to handle isakmp in Phase2
692 ike_session_newph2(unsigned int version
, int type
)
694 phase2_handle_t
*iph2
= NULL
;
696 /* create new iph2 */
697 iph2
= racoon_calloc(1, sizeof(*iph2
));
700 iph2
->version
= version
;
701 iph2
->phase2_type
= type
;
704 plog(ASL_LEVEL_DEBUG
, "*** New Phase 2\n");
709 * initialize ph2handle
710 * NOTE: don't initialize src/dst.
711 * SPI in the proposal is cleared.
714 ike_session_initph2(phase2_handle_t
*iph2
)
716 sched_scrub_param(iph2
);
720 VPTRINIT(iph2
->sendbuf
);
721 VPTRINIT(iph2
->msg1
);
723 /* clear spi, keep variables in the proposal */
724 if (iph2
->proposal
) {
726 for (pr
= iph2
->proposal
->head
; pr
!= NULL
; pr
= pr
->next
)
731 if (iph2
->approval
) {
732 flushsaprop(iph2
->approval
);
733 iph2
->approval
= NULL
;
736 /* clear the generated policy */
737 if (iph2
->spidx_gen
) {
738 delsp_bothdir(iph2
->spidx_gen
);
739 racoon_free(iph2
->spidx_gen
);
740 iph2
->spidx_gen
= NULL
;
744 oakley_dhgrp_free(iph2
->pfsgrp
);
748 VPTRINIT(iph2
->dhpriv
);
749 VPTRINIT(iph2
->dhpub
);
750 VPTRINIT(iph2
->dhpub_p
);
751 VPTRINIT(iph2
->dhgxy
);
753 VPTRINIT(iph2
->id_p
);
754 VPTRINIT(iph2
->nonce
);
755 VPTRINIT(iph2
->nonce_p
);
757 VPTRINIT(iph2
->sa_ret
);
760 oakley_delivm(iph2
->ivm
);
766 * delete new isakmp Phase 2 status record to handle isakmp in Phase2
769 ike_session_delph2(phase2_handle_t
*iph2
)
771 ike_session_initph2(iph2
);
774 racoon_free(iph2
->src
);
778 racoon_free(iph2
->dst
);
782 racoon_free(iph2
->src_id
);
786 racoon_free(iph2
->dst_id
);
790 if (iph2
->proposal
) {
791 flushsaprop(iph2
->proposal
);
792 iph2
->proposal
= NULL
;
796 release_sainfo(iph2
->sainfo
);
800 VPTRINIT(iph2
->id_p
);
801 VPTRINIT(iph2
->ext_nat_id
);
802 VPTRINIT(iph2
->ext_nat_id_p
);
805 SCHED_KILL(iph2
->sce
);
807 SCHED_KILL(iph2
->scr
);
813 ike_session_flush_all_phase2_for_session(ike_session_t
*session
, int ignore_estab_or_assert_handles
)
815 phase2_handle_t
*p
= NULL
;
816 phase2_handle_t
*next
= NULL
;
817 LIST_FOREACH_SAFE(p
, &session
->ph2tree
, ph2ofsession_chain
, next
) {
818 if (p
->is_dying
|| FSM_STATE_IS_EXPIRED(p
->status
)) {
821 if (ignore_estab_or_assert_handles
&& p
->parent_session
&& !p
->parent_session
->stopped_by_vpn_controller
&& p
->parent_session
->is_asserted
) {
822 plog(ASL_LEVEL_DEBUG
,
823 "skipping phase2 handle that's asserted...\n");
826 if (FSM_STATE_IS_ESTABLISHED(p
->status
)){
827 if (ignore_estab_or_assert_handles
) {
828 plog(ASL_LEVEL_DEBUG
,
829 "skipping ph2 handler that's established...\n");
832 /* send delete information */
833 plog(ASL_LEVEL_DEBUG
,
834 "got an established ph2 handler to flush...\n");
835 isakmp_info_send_d2(p
);
837 plog(ASL_LEVEL_DEBUG
,
838 "got a ph2 handler to flush (state %d)\n", p
->status
);
841 ike_session_stopped_by_controller(p
->parent_session
,
842 ike_session_stopped_by_flush
);
844 ike_session_unlink_phase2(p
);
849 ike_session_flush_all_phase2(int ignore_estab_or_assert_handles
)
851 ike_session_t
*session
= NULL
;
852 ike_session_t
*next_session
= NULL
;
854 plog(ASL_LEVEL_DEBUG
,
855 "flushing ph2 handles: ignore_estab_or_assert %d...\n", ignore_estab_or_assert_handles
);
857 LIST_FOREACH_SAFE(session
, &ike_session_tree
, chain
, next_session
) {
858 ike_session_flush_all_phase2_for_session(session
, ignore_estab_or_assert_handles
);
863 * Delete all Phase 2 handlers for this src/dst/proto. This
864 * is used during INITIAL-CONTACT processing (so no need to
865 * send a message to the peer).
867 //%%%%%%%%%%%%%%%%%%% make this smarter - find session using addresses ????
869 ike_session_deleteallph2(struct sockaddr_storage
*src
, struct sockaddr_storage
*dst
, u_int proto_id
)
871 ike_session_t
*session
= NULL
;
872 ike_session_t
*next_session
= NULL
;
873 phase2_handle_t
*iph2
= NULL
;
874 phase2_handle_t
*next_iph2
= NULL
;
877 LIST_FOREACH_SAFE(session
, &ike_session_tree
, chain
, next_session
) {
878 LIST_FOREACH_SAFE(iph2
, &session
->ph2tree
, ph2ofsession_chain
, next_iph2
) {
879 if (iph2
->is_dying
|| FSM_STATE_IS_EXPIRED(iph2
->status
)) {
882 if (iph2
->proposal
== NULL
&& iph2
->approval
== NULL
)
884 if (cmpsaddrwop(src
, iph2
->src
) != 0 ||
885 cmpsaddrwop(dst
, iph2
->dst
) != 0) {
888 if (iph2
->approval
!= NULL
) {
889 for (pr
= iph2
->approval
->head
; pr
!= NULL
;
891 if (proto_id
== pr
->proto_id
)
894 } else if (iph2
->proposal
!= NULL
) {
895 for (pr
= iph2
->proposal
->head
; pr
!= NULL
;
897 if (proto_id
== pr
->proto_id
)
903 plog(ASL_LEVEL_DEBUG
,
904 "deleteallph2: got a ph2 handler...\n");
905 if (FSM_STATE_IS_ESTABLISHED(iph2
->status
))
906 isakmp_info_send_d2(iph2
);
907 ike_session_stopped_by_controller(iph2
->parent_session
,
908 ike_session_stopped_by_flush
);
909 ike_session_unlink_phase2(iph2
);
915 * Delete all Phase 1 handlers for this src/dst.
918 ike_session_deleteallph1(struct sockaddr_storage
*src
, struct sockaddr_storage
*dst
)
920 ike_session_t
*session
= NULL
;
921 ike_session_t
*next_session
= NULL
;
922 phase1_handle_t
*iph1
= NULL
;
923 phase1_handle_t
*next_iph1
= NULL
;
925 LIST_FOREACH_SAFE(session
, &ike_session_tree
, chain
, next_session
) {
926 LIST_FOREACH_SAFE(iph1
, &session
->ph1tree
, ph1ofsession_chain
, next_iph1
) {
927 if (cmpsaddrwop(src
, iph1
->local
) != 0 ||
928 cmpsaddrwop(dst
, iph1
->remote
) != 0) {
931 plog(ASL_LEVEL_DEBUG
,
932 "deleteallph1: got a ph1 handler...\n");
933 if (FSM_STATE_IS_ESTABLISHED(iph1
->status
))
934 isakmp_info_send_d1(iph1
);
936 ike_session_stopped_by_controller(iph1
->parent_session
, ike_session_stopped_by_flush
);
937 ike_session_unlink_phase1(iph1
);
943 /* %%% management contacted list */
945 * search contacted list.
948 ike_session_getcontacted(remote
)
949 struct sockaddr_storage
*remote
;
953 LIST_FOREACH(p
, &ctdtree
, chain
) {
954 if (cmpsaddrstrict(remote
, p
->remote
) == 0)
962 * create new isakmp Phase 2 status record to handle isakmp in Phase2
965 ike_session_inscontacted(remote
)
966 struct sockaddr_storage
*remote
;
968 struct contacted
*new;
970 /* create new iph2 */
971 new = racoon_calloc(1, sizeof(*new));
975 new->remote
= dupsaddr(remote
);
976 if (new->remote
== NULL
) {
978 "failed to allocate buffer.\n");
983 LIST_INSERT_HEAD(&ctdtree
, new, chain
);
990 ike_session_clear_contacted()
992 struct contacted
*c
, *next
;
993 LIST_FOREACH_SAFE(c
, &ctdtree
, chain
, next
) {
994 LIST_REMOVE(c
, chain
);
995 racoon_free(c
->remote
);
1001 ike_session_initctdtree()
1003 LIST_INIT(&ctdtree
);
1007 ike_session_get_exp_retx_interval (int num_retries
, int fixed_retry_interval
)
1009 // first 3 retries aren't exponential
1010 if (num_retries
<= 3) {
1011 return (time_t)fixed_retry_interval
;
1013 return (time_t)(num_retries
* fixed_retry_interval
);
1018 * check the response has been sent to the peer. when not, simply reply
1019 * the buffered packet to the peer.
1021 * 0: the packet is received at the first time.
1022 * 1: the packet was processed before.
1023 * 2: the packet was processed before, but the address mismatches.
1024 * -1: error happened.
1027 ike_session_check_recvdpkt(remote
, local
, rbuf
)
1028 struct sockaddr_storage
*remote
, *local
;
1036 /* set current time */
1039 hash
= eay_md5_one(rbuf
);
1042 "failed to allocate buffer.\n");
1046 LIST_FOREACH(r
, &rcptree
, chain
) {
1047 if (memcmp(hash
->v
, r
->hash
->v
, r
->hash
->l
) == 0)
1052 /* this is the first time to receive the packet */
1057 * the packet was processed before, but the remote address mismatches.
1058 * ignore the port to accomodate port changes (e.g. floating).
1060 if (cmpsaddrwop(remote
, r
->remote
) != 0) {
1065 * it should not check the local address because the packet
1066 * may arrive at other interface.
1069 /* check the previous time to send */
1070 if (t
- r
->time_send
< 1) {
1071 plog(ASL_LEVEL_WARNING
,
1072 "the packet retransmitted in a short time from %s\n",
1073 saddr2str((struct sockaddr
*)remote
));
1074 /*XXX should it be error ? */
1077 /* select the socket to be sent */
1078 s
= getsockmyaddr((struct sockaddr
*)r
->local
);
1082 // don't send if we recently sent a response.
1083 if (r
->time_send
&& t
> r
->time_send
) {
1084 d
= t
- r
->time_send
;
1085 if (d
< r
->retry_interval
) {
1086 plog(ASL_LEVEL_ERR
, "already responded within the past %ld secs\n", d
);
1092 if (r
->frag_flags
&& r
->sendbuf
->l
> ISAKMP_FRAG_MAXLEN
) {
1093 /* resend the packet if needed */
1094 plog(ASL_LEVEL_ERR
, "!!! retransmitting frags\n");
1095 len
= sendfragsfromto(s
, r
->sendbuf
,
1096 r
->local
, r
->remote
, lcconf
->count_persend
,
1099 plog(ASL_LEVEL_ERR
, "!!! skipped retransmitting frags: frag_flags %x, r->sendbuf->l %zu, max %d\n", r
->frag_flags
, r
->sendbuf
->l
, ISAKMP_FRAG_MAXLEN
);
1100 /* resend the packet if needed */
1101 len
= sendfromto(s
, r
->sendbuf
->v
, r
->sendbuf
->l
,
1102 r
->local
, r
->remote
, lcconf
->count_persend
);
1105 /* resend the packet if needed */
1106 len
= sendfromto(s
, r
->sendbuf
->v
, r
->sendbuf
->l
,
1107 r
->local
, r
->remote
, lcconf
->count_persend
);
1110 plog(ASL_LEVEL_ERR
, "sendfromto failed\n");
1114 /* check the retry counter */
1116 if (r
->retry_counter
<= 0) {
1117 ike_session_rem_recvdpkt(r
);
1118 ike_session_del_recvdpkt(r
);
1119 plog(ASL_LEVEL_DEBUG
,
1120 "deleted the retransmission packet to %s.\n",
1121 saddr2str((struct sockaddr
*)remote
));
1124 r
->retry_interval
= ike_session_get_exp_retx_interval((lcconf
->retry_counter
- r
->retry_counter
),
1125 lcconf
->retry_interval
);
1132 * adding a hash of received packet into the received list.
1135 ike_session_add_recvdpkt(remote
, local
, sbuf
, rbuf
, non_esp
, frag_flags
)
1136 struct sockaddr_storage
*remote
, *local
;
1137 vchar_t
*sbuf
, *rbuf
;
1139 u_int32_t frag_flags
;
1141 struct recvdpkt
*new = NULL
;
1143 if (lcconf
->retry_counter
== 0) {
1144 /* no need to add it */
1148 new = racoon_calloc(1, sizeof(*new));
1151 "failed to allocate buffer.\n");
1155 new->hash
= eay_md5_one(rbuf
);
1158 "failed to allocate buffer.\n");
1159 ike_session_del_recvdpkt(new);
1162 new->remote
= dupsaddr(remote
);
1163 if (new->remote
== NULL
) {
1165 "failed to allocate buffer.\n");
1166 ike_session_del_recvdpkt(new);
1169 new->local
= dupsaddr(local
);
1170 if (new->local
== NULL
) {
1172 "failed to allocate buffer.\n");
1173 ike_session_del_recvdpkt(new);
1178 plog (ASL_LEVEL_DEBUG
, "Adding NON-ESP marker\n");
1180 /* If NAT-T port floating is in use, 4 zero bytes (non-ESP marker)
1181 must added just before the packet itself. For this we must
1182 allocate a new buffer and release it at the end. */
1183 if ((new->sendbuf
= vmalloc (sbuf
->l
+ non_esp
)) == NULL
) {
1185 "failed to allocate extra buf for non-esp\n");
1186 ike_session_del_recvdpkt(new);
1189 *ALIGNED_CAST(u_int32_t
*)new->sendbuf
->v
= 0;
1190 memcpy(new->sendbuf
->v
+ non_esp
, sbuf
->v
, sbuf
->l
);
1192 new->sendbuf
= vdup(sbuf
);
1193 if (new->sendbuf
== NULL
) {
1195 "failed to allocate buffer.\n");
1196 ike_session_del_recvdpkt(new);
1201 new->retry_counter
= lcconf
->retry_counter
;
1203 new->created
= time(NULL
);
1206 new->frag_flags
= frag_flags
;
1209 new->retry_interval
= ike_session_get_exp_retx_interval((lcconf
->retry_counter
- new->retry_counter
),
1210 lcconf
->retry_interval
);
1212 LIST_INSERT_HEAD(&rcptree
, new, chain
);
1218 ike_session_del_recvdpkt(r
)
1222 racoon_free(r
->remote
);
1224 racoon_free(r
->local
);
1233 ike_session_rem_recvdpkt(r
)
1236 LIST_REMOVE(r
, chain
);
1240 sweep_recvdpkt(dummy
)
1243 struct recvdpkt
*r
, *next
;
1246 /* set current time */
1249 /* set the lifetime of the retransmission */
1250 lt
= lcconf
->retry_counter
* lcconf
->retry_interval
;
1252 LIST_FOREACH_SAFE(r
, &rcptree
, chain
, next
) {
1253 if (t
- r
->created
> lt
) {
1254 ike_session_rem_recvdpkt(r
);
1255 ike_session_del_recvdpkt(r
);
1259 sched_new(lt
, sweep_recvdpkt
, &rcptree
);
1263 ike_session_clear_recvdpkt()
1265 struct recvdpkt
*r
, *next
;
1267 LIST_FOREACH_SAFE(r
, &rcptree
, chain
, next
) {
1268 ike_session_rem_recvdpkt(r
);
1269 ike_session_del_recvdpkt(r
);
1271 sched_scrub_param(&rcptree
);
1275 ike_session_init_recvdpkt()
1277 time_t lt
= lcconf
->retry_counter
* lcconf
->retry_interval
;
1279 LIST_INIT(&rcptree
);
1281 sched_new(lt
, sweep_recvdpkt
, &rcptree
);
1285 #ifdef ENABLE_HYBRID
1287 * Returns 0 if the address was obtained by ISAKMP mode config, 1 otherwise
1288 * This should be in isakmp_cfg.c but ph1tree being private, it must be there
1291 exclude_cfg_addr(const struct sockaddr_storage
*addr
)
1293 ike_session_t
*session
;
1295 struct sockaddr_in
*sin
;
1297 LIST_FOREACH(session
, &ike_session_tree
, chain
) {
1298 LIST_FOREACH(p
, &session
->ph1tree
, chain
) {
1299 if ((p
->mode_cfg
!= NULL
) &&
1300 (p
->mode_cfg
->flags
& ISAKMP_CFG_GOT_ADDR4
) &&
1301 (addr
->ss_family
== AF_INET
)) {
1302 sin
= (struct sockaddr_in
*)addr
;
1303 if (sin
->sin_addr
.s_addr
== p
->mode_cfg
->addr4
.s_addr
)
1312 #endif /* NOT_USED */
1315 ike_session_expire_session(ike_session_t
*session
)
1319 phase1_handle_t
*next
;
1320 phase2_handle_t
*p2
;
1322 if (session
== NULL
)
1325 LIST_FOREACH(p2
, &session
->ph2tree
, ph2ofsession_chain
) {
1326 if (p2
->is_dying
|| FSM_STATE_IS_EXPIRED(p2
->status
)) {
1330 // Don't send a delete, since the ph1 implies the removal of ph2s
1331 isakmp_ph2expire(p2
);
1335 LIST_FOREACH_SAFE(p
, &session
->ph1tree
, ph1ofsession_chain
, next
) {
1336 if (p
->is_dying
|| FSM_STATE_IS_EXPIRED(p
->status
)) {
1340 ike_session_purge_ph2s_by_ph1(p
);
1341 if (FSM_STATE_IS_ESTABLISHED(p
->status
))
1342 isakmp_info_send_d1(p
);
1343 isakmp_ph1expire(p
);
1350 #ifdef ENABLE_HYBRID
1352 ike_session_purgephXbydstaddrwop(struct sockaddr_storage
*remote
)
1355 ike_session_t
*session
= NULL
;
1356 ike_session_t
*next_session
= NULL
;
1358 phase2_handle_t
*p2
;
1360 LIST_FOREACH_SAFE(session
, &ike_session_tree
, chain
, next_session
) {
1361 LIST_FOREACH(p2
, &session
->ph2tree
, ph2ofsession_chain
) {
1362 if (p2
->is_dying
|| FSM_STATE_IS_EXPIRED(p2
->status
)) {
1365 if (cmpsaddrwop(remote
, p2
->dst
) == 0) {
1366 plog(ASL_LEVEL_DEBUG
,
1367 "in %s... purging Phase 2 structures\n", __FUNCTION__
);
1368 if (FSM_STATE_IS_ESTABLISHED(p2
->status
))
1369 isakmp_info_send_d2(p2
);
1370 isakmp_ph2expire(p2
);
1375 LIST_FOREACH(p
, &session
->ph1tree
, ph1ofsession_chain
) {
1376 if (p
->is_dying
|| FSM_STATE_IS_EXPIRED(p
->status
)) {
1379 if (cmpsaddrwop(remote
, p
->remote
) == 0) {
1380 plog(ASL_LEVEL_DEBUG
,
1381 "in %s... purging Phase 1 and related Phase 2 structures\n", __FUNCTION__
);
1382 ike_session_purge_ph2s_by_ph1(p
);
1383 if (FSM_STATE_IS_ESTABLISHED(p
->status
))
1384 isakmp_info_send_d1(p
);
1385 isakmp_ph1expire(p
);
1395 ike_session_purgephXbyspid(u_int32_t spid
, int del_boundph1
)
1397 ike_session_t
*session
= NULL
;
1398 ike_session_t
*next_session
= NULL
;
1399 phase2_handle_t
*iph2
= NULL
;
1400 phase2_handle_t
*next_iph2
= NULL
;
1401 phase1_handle_t
*iph1
= NULL
;
1402 phase1_handle_t
*next_iph1
= NULL
;
1404 LIST_FOREACH_SAFE(session
, &ike_session_tree
, chain
, next_session
) {
1405 // do ph2's first... we need the ph1s for notifications
1406 LIST_FOREACH_SAFE(iph2
, &session
->ph2tree
, ph2ofsession_chain
, next_iph2
) {
1407 if (spid
== iph2
->spid
) {
1408 if (iph2
->is_dying
|| FSM_STATE_IS_EXPIRED(iph2
->status
)) {
1411 if (FSM_STATE_IS_ESTABLISHED(iph2
->status
)) {
1412 isakmp_info_send_d2(iph2
);
1414 ike_session_stopped_by_controller(iph2
->parent_session
,
1415 ike_session_stopped_by_flush
);
1416 isakmp_ph2expire(iph2
); // iph2 will go down 1 second later.
1420 // do the ph1s last. %%%%%%%%%%%%%%%%%% re-organize this - check del_boundph1 first
1421 LIST_FOREACH_SAFE(iph2
, &session
->ph2tree
, ph2ofsession_chain
, next_iph2
) {
1422 if (spid
== iph2
->spid
) {
1423 if (del_boundph1
&& iph2
->parent_session
) {
1424 LIST_FOREACH_SAFE(iph1
, &iph2
->parent_session
->ph1tree
, ph1ofsession_chain
, next_iph1
) {
1425 if (iph1
->is_dying
|| FSM_STATE_IS_EXPIRED(iph1
->status
)) {
1428 if (FSM_STATE_IS_ESTABLISHED(iph1
->status
)) {
1429 isakmp_info_send_d1(iph1
);
1431 isakmp_ph1expire(iph1
);
1443 ike_session_ph1_force_dpd (struct sockaddr_storage
*remote
)
1446 ike_session_t
*session
= NULL
;
1447 phase1_handle_t
*p
= NULL
;
1449 LIST_FOREACH(session
, &ike_session_tree
, chain
) {
1450 LIST_FOREACH(p
, &session
->ph1tree
, ph1ofsession_chain
) {
1451 if (cmpsaddrwop(remote
, p
->remote
) == 0) {
1452 if (FSM_STATE_IS_ESTABLISHED(p
->status
) &&
1455 p
->rmconf
->dpd_interval
) {
1457 isakmp_info_send_r_u(p
);
1460 plog(ASL_LEVEL_DEBUG
, "Skipping forced-DPD for Phase 1 (dpd already in progress).\n");
1462 if (p
->parent_session
) {
1463 p
->parent_session
->controller_awaiting_peer_resp
= 1;
1466 plog(ASL_LEVEL_DEBUG
, "Skipping forced-DPD for Phase 1 (status %d, dying %d, dpd-support %d, dpd-interval %d).\n",
1467 p
->status
, p
->is_dying
, p
->dpd_support
, p
->rmconf
->dpd_interval
);
1478 sweep_sleepwake(void)
1480 ike_session_t
*session
= NULL
;
1481 ike_session_t
*next_session
= NULL
;
1482 phase2_handle_t
*iph2
= NULL
;
1483 phase2_handle_t
*next_iph2
= NULL
;
1484 phase1_handle_t
*iph1
= NULL
;
1485 phase1_handle_t
*next_iph1
= NULL
;
1487 LIST_FOREACH_SAFE(session
, &ike_session_tree
, chain
, next_session
) {
1489 LIST_FOREACH_SAFE(iph1
, &session
->ph1tree
, ph1ofsession_chain
, next_iph1
) {
1490 if (iph1
->parent_session
&& iph1
->parent_session
->is_asserted
) {
1491 plog(ASL_LEVEL_DEBUG
, "Skipping sweep of Phase 1 %s because it's been asserted.\n",
1492 isakmp_pindex(&iph1
->index
, 0));
1495 if (iph1
->is_dying
|| FSM_STATE_IS_EXPIRED(iph1
->status
)) {
1496 plog(ASL_LEVEL_DEBUG
, "Skipping sweep of Phase 1 %s because it's already expired.\n",
1497 isakmp_pindex(&iph1
->index
, 0));
1502 if (sched_get_time(iph1
->sce
, &xtime
)) {
1503 if (xtime
<= swept_at
) {
1504 SCHED_KILL(iph1
->sce
);
1505 SCHED_KILL(iph1
->sce_rekey
);
1507 fsm_set_state(&iph1
->status
, IKEV1_STATE_PHASE1_EXPIRED
);
1508 ike_session_update_ph1_ph2tree(iph1
); // move unbind/rebind ph2s to from current ph1
1509 iph1
->sce
= sched_new(1, isakmp_ph1delete_stub
, iph1
);
1510 plog(ASL_LEVEL_DEBUG
, "Phase 1 %s expired while sleeping: quick deletion.\n",
1511 isakmp_pindex(&iph1
->index
, 0));
1515 if (iph1
->sce_rekey
) {
1517 if (sched_get_time(iph1
->sce_rekey
, &xtime
)) {
1518 if (FSM_STATE_IS_EXPIRED(iph1
->status
) || xtime
<= swept_at
) {
1519 SCHED_KILL(iph1
->sce_rekey
);
1525 if (sched_get_time(iph1
->scr
, &xtime
)) {
1526 if (FSM_STATE_IS_EXPIRED(iph1
->status
) || xtime
<= swept_at
) {
1527 SCHED_KILL(iph1
->scr
);
1532 if (iph1
->dpd_r_u
) {
1534 if (sched_get_time(iph1
->dpd_r_u
, &xtime
)) {
1535 if (FSM_STATE_IS_EXPIRED(iph1
->status
) || xtime
<= swept_at
) {
1536 SCHED_KILL(iph1
->dpd_r_u
);
1544 LIST_FOREACH_SAFE(iph2
, &session
->ph2tree
, ph2ofsession_chain
, next_iph2
) {
1545 if (iph2
->parent_session
&& iph2
->parent_session
->is_asserted
) {
1546 plog(ASL_LEVEL_DEBUG
, "Skipping sweep of Phase 2 because it's been asserted.\n");
1549 if (iph2
->is_dying
|| FSM_STATE_IS_EXPIRED(iph2
->status
)) {
1550 plog(ASL_LEVEL_DEBUG
, "Skipping sweep of Phase 2 because it's already expired.\n");
1555 if (sched_get_time(iph2
->sce
, &xtime
)) {
1556 if (xtime
<= swept_at
) {
1557 fsm_set_state(&iph2
->status
, IKEV1_STATE_PHASE2_EXPIRED
);
1559 isakmp_ph2expire(iph2
); // iph2 will go down 1 second later.
1560 ike_session_stopped_by_controller(iph2
->parent_session
,
1561 ike_session_stopped_by_sleepwake
);
1562 plog(ASL_LEVEL_DEBUG
, "Phase 2 expired while sleeping: quick deletion.\n");
1568 if (sched_get_time(iph2
->scr
, &xtime
)) {
1569 if (FSM_STATE_IS_EXPIRED(iph2
->status
) || xtime
<= swept_at
) {
1570 SCHED_KILL(iph2
->scr
);
1576 //%%%%%%%%%%%%%%% fix this
1577 // do the ike_session last
1578 ike_session_sweep_sleepwake();
1582 iph1_get_remote_v4_address(phase1_handle_t
*iph1
)
1584 uint32_t address
= 0;
1585 if (iph1
->remote
->ss_family
== AF_INET
) {
1586 address
= ((struct sockaddr_in
*)iph1
->remote
)->sin_addr
.s_addr
;
1587 } else if (iph1
->remote
->ss_family
== AF_INET6
&&
1588 iph1
->nat64_prefix
.length
) {
1589 if (!nw_nat64_extract_v4(&iph1
->nat64_prefix
, &((struct sockaddr_in6
*)iph1
->remote
)->sin6_addr
, (struct in_addr
*)&address
)) {
1590 plog(ASL_LEVEL_ERR
, "Failed to extract IPv4 from Phase 1 IPv6 address.\n");
1593 plog(ASL_LEVEL_ERR
, "Failed to get IPv4 address for Phase 1 (family=%u, NAT64Prefix=%u)\n",
1594 iph1
->remote
->ss_family
,
1595 iph1
->nat64_prefix
.length
);
1601 iph2_get_remote_v4_address(phase2_handle_t
*iph2
)
1603 uint32_t address
= 0;
1604 if (iph2
->dst
->ss_family
== AF_INET
) {
1605 address
= ((struct sockaddr_in
*)iph2
->dst
)->sin_addr
.s_addr
;
1606 } else if (iph2
->dst
->ss_family
== AF_INET6
&&
1607 iph2
->nat64_prefix
.length
) {
1608 if (!nw_nat64_extract_v4(&iph2
->nat64_prefix
, &((struct sockaddr_in6
*)iph2
->dst
)->sin6_addr
, (struct in_addr
*)&address
)) {
1609 plog(ASL_LEVEL_ERR
, "Failed to extract IPv4 from Phase 2 IPv6 address.\n");
1612 plog(ASL_LEVEL_ERR
, "Failed to get IPv4 address for Phase 2 (family=%u, NAT64Prefix=%u)\n",
1613 iph2
->dst
->ss_family
,
1614 iph2
->nat64_prefix
.length
);