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>
55 #include "grabmyaddr.h"
56 #include "algorithm.h"
57 #include "crypto_openssl.h"
60 #include "isakmp_var.h"
63 #include "isakmp_xauth.h"
64 #include "isakmp_cfg.h"
66 #include "isakmp_inf.h"
68 #include "remoteconf.h"
69 #include "localconf.h"
72 #include "nattraversal.h"
73 #include "ike_session.h"
74 #include "isakmp_frag.h"
78 #include "power_mgmt.h"
80 extern LIST_HEAD(_ike_session_tree_
, ike_session
) ike_session_tree
;
81 static LIST_HEAD(_ctdtree_
, contacted
) ctdtree
;
82 static LIST_HEAD(_rcptree_
, recvdpkt
) rcptree
;
84 static void ike_session_del_recvdpkt (struct recvdpkt
*);
85 static void ike_session_rem_recvdpkt (struct recvdpkt
*);
86 static void sweep_recvdpkt (void *);
89 * functions about management of the isakmp status table
91 /* %%% management phase 1 handler */
93 * search for isakmpsa handler with isakmp index.
96 extern caddr_t
val2str (const char *, size_t);
98 static phase1_handle_t
*
99 getph1byindex(ike_session_t
*session
, isakmp_index
*index
)
101 phase1_handle_t
*p
= NULL
;
103 LIST_FOREACH(p
, &session
->ph1tree
, ph1ofsession_chain
) {
104 if (FSM_STATE_IS_EXPIRED(p
->status
))
106 if (memcmp(&p
->index
, index
, sizeof(*index
)) == 0)
114 ike_session_getph1byindex(ike_session_t
*session
, isakmp_index
*index
)
117 ike_session_t
*cur_session
= NULL
;
120 return getph1byindex(session
, index
);
122 LIST_FOREACH(cur_session
, &ike_session_tree
, chain
) {
123 if ((p
= getph1byindex(cur_session
, index
)) != NULL
)
131 * search for isakmp handler by i_ck in index.
134 static phase1_handle_t
*
135 getph1byindex0 (ike_session_t
*session
, isakmp_index
*index
)
137 phase1_handle_t
*p
= NULL
;
139 LIST_FOREACH(p
, &session
->ph1tree
, ph1ofsession_chain
) {
140 if (FSM_STATE_IS_EXPIRED(p
->status
))
142 if (memcmp(&p
->index
.i_ck
, &index
->i_ck
, sizeof(cookie_t
)) == 0)
149 ike_session_getph1byindex0(ike_session_t
*session
, isakmp_index
*index
)
151 phase1_handle_t
*p
= NULL
;
152 ike_session_t
*cur_session
= NULL
;
155 return getph1byindex0(session
, index
);
157 LIST_FOREACH(cur_session
, &ike_session_tree
, chain
) {
158 if ((p
= getph1byindex0(cur_session
, index
)) != NULL
)
166 * search for isakmpsa handler by source and remote address.
167 * don't use port number to search because this function search
168 * with phase 2's destinaion.
171 ike_session_getph1byaddr(ike_session_t
*session
, struct sockaddr_storage
*local
, struct sockaddr_storage
*remote
)
173 phase1_handle_t
*p
= NULL
;
175 plog(ASL_LEVEL_DEBUG
, "getph1byaddr: start\n");
176 plog(ASL_LEVEL_DEBUG
, "local: %s\n", saddr2str((struct sockaddr
*)local
));
177 plog(ASL_LEVEL_DEBUG
, "remote: %s\n", saddr2str((struct sockaddr
*)remote
));
179 LIST_FOREACH(p
, &session
->ph1tree
, ph1ofsession_chain
) {
180 if (FSM_STATE_IS_EXPIRED(p
->status
))
182 plog(ASL_LEVEL_DEBUG
, "p->local: %s\n", saddr2str((struct sockaddr
*)p
->local
));
183 plog(ASL_LEVEL_DEBUG
, "p->remote: %s\n", saddr2str((struct sockaddr
*)p
->remote
));
184 if (CMPSADDR(local
, p
->local
) == 0
185 && CMPSADDR(remote
, p
->remote
) == 0){
186 plog(ASL_LEVEL_DEBUG
, "matched\n");
191 plog(ASL_LEVEL_DEBUG
, "no match\n");
196 static phase1_handle_t
*
197 sgetph1byaddrwop(ike_session_t
*session
, struct sockaddr_storage
*local
, struct sockaddr_storage
*remote
)
199 phase1_handle_t
*p
= NULL
;
201 LIST_FOREACH(p
, &session
->ph1tree
, ph1ofsession_chain
) {
202 if (FSM_STATE_IS_EXPIRED(p
->status
))
204 if (cmpsaddrwop(local
, p
->local
) == 0
205 && cmpsaddrwop(remote
, p
->remote
) == 0)
213 ike_session_getph1byaddrwop(ike_session_t
*session
, struct sockaddr_storage
*local
, struct sockaddr_storage
*remote
)
216 ike_session_t
*cur_session
= NULL
;
219 return sgetph1byaddrwop(session
, local
, remote
);
221 LIST_FOREACH(cur_session
, &ike_session_tree
, chain
) {
222 if ((p
= sgetph1byaddrwop(cur_session
, local
, remote
)) != NULL
)
230 * search for isakmpsa handler by remote address.
231 * don't use port number to search because this function search
232 * with phase 2's destinaion.
235 sike_session_getph1bydstaddrwop(ike_session_t
*session
, struct sockaddr_storage
*remote
)
237 phase1_handle_t
*p
= NULL
;
239 LIST_FOREACH(p
, &session
->ph1tree
, ph1ofsession_chain
) {
240 if (FSM_STATE_IS_EXPIRED(p
->status
)) {
243 if (remote
->ss_family
== AF_INET
&&
244 p
->nat64_prefix
.length
) {
245 struct in_addr address
;
246 nw_nat64_extract_v4(&p
->nat64_prefix
, &((struct sockaddr_in6
*)p
->remote
)->sin6_addr
, &address
);
247 if (((struct sockaddr_in
*)remote
)->sin_addr
.s_addr
== address
.s_addr
) {
250 } else if (cmpsaddrwop(remote
, p
->remote
) == 0) {
259 ike_session_getph1bydstaddrwop(ike_session_t
*session
, struct sockaddr_storage
*remote
)
262 ike_session_t
*cur_session
= NULL
;
265 return sike_session_getph1bydstaddrwop(session
, remote
);
267 LIST_FOREACH(cur_session
, &ike_session_tree
, chain
) {
268 if ((p
= sike_session_getph1bydstaddrwop(cur_session
, remote
)) != NULL
)
276 ike_session_islast_ph1(phase1_handle_t
*ph1
)
278 phase1_handle_t
*p
= NULL
;
280 LIST_FOREACH(p
, &ph1
->parent_session
->ph1tree
, ph1ofsession_chain
) {
281 if (p
->is_dying
|| FSM_STATE_IS_EXPIRED(p
->status
))
283 if (CMPSADDR(ph1
->remote
, p
->remote
) == 0) {
293 * create new isakmp Phase 1 status record to handle isakmp in Phase1
296 ike_session_newph1(unsigned int version
)
298 phase1_handle_t
*iph1
;
300 /* create new iph1 */
301 iph1
= racoon_calloc(1, sizeof(*iph1
));
304 iph1
->version
= version
;
307 iph1
->dpd_support
= 0;
308 iph1
->dpd_lastack
= 0;
311 iph1
->peer_sent_ike
= 0;
312 iph1
->dpd_r_u
= NULL
;
314 #ifdef ENABLE_VPNCONTROL_PORT
315 iph1
->ping_sched
= NULL
;
318 plog(ASL_LEVEL_NOTICE
, "New Phase 1\n");
323 * delete new isakmp Phase 1 status record to handle isakmp in Phase1
326 ike_session_delph1(phase1_handle_t
*iph1
)
332 if (iph1
->natt_options
) {
333 racoon_free(iph1
->natt_options
);
334 iph1
->natt_options
= NULL
;
340 isakmp_cfg_rmstate(iph1
);
341 VPTRINIT(iph1
->xauth_awaiting_userinput_msg
);
346 SCHED_KILL(iph1
->dpd_r_u
);
348 #ifdef ENABLE_VPNCONTROL_PORT
349 if (iph1
->ping_sched
)
350 SCHED_KILL(iph1
->ping_sched
);
354 racoon_free(iph1
->remote
);
358 racoon_free(iph1
->local
);
362 if (iph1
->approval
) {
363 delisakmpsa(iph1
->approval
);
364 iph1
->approval
= NULL
;
367 sched_scrub_param(iph1
);
369 SCHED_KILL(iph1
->sce
);
371 SCHED_KILL(iph1
->sce_rekey
);
373 SCHED_KILL(iph1
->scr
);
375 VPTRINIT(iph1
->sendbuf
);
377 VPTRINIT(iph1
->dhpriv
);
378 VPTRINIT(iph1
->dhpub
);
379 VPTRINIT(iph1
->dhpub_p
);
380 VPTRINIT(iph1
->dhgxy
);
381 VPTRINIT(iph1
->nonce
);
382 VPTRINIT(iph1
->nonce_p
);
383 VPTRINIT(iph1
->skeyid
);
384 VPTRINIT(iph1
->skeyid_d
);
385 VPTRINIT(iph1
->skeyid_a
);
386 VPTRINIT(iph1
->skeyid_a_p
);
387 VPTRINIT(iph1
->skeyid_e
);
388 VPTRINIT(iph1
->skeyid_e_p
);
390 VPTRINIT(iph1
->key_p
);
391 VPTRINIT(iph1
->hash
);
393 VPTRINIT(iph1
->sig_p
);
394 oakley_delcert(iph1
->cert
);
396 oakley_delcert(iph1
->cert_p
);
398 oakley_delcert(iph1
->crl_p
);
400 oakley_delcert(iph1
->cr_p
);
403 VPTRINIT(iph1
->id_p
);
405 if(iph1
->approval
!= NULL
)
406 delisakmpsa(iph1
->approval
);
409 oakley_delivm(iph1
->ivm
);
414 VPTRINIT(iph1
->sa_ret
);
417 release_rmconf(iph1
->rmconf
);
425 ike_session_flush_all_phase1_for_session(ike_session_t
*session
, int ignore_estab_or_assert_handles
)
427 phase1_handle_t
*p
, *next
;
429 LIST_FOREACH_SAFE(p
, &session
->ph1tree
, ph1ofsession_chain
, next
) {
430 if (ignore_estab_or_assert_handles
&& p
->parent_session
&& !p
->parent_session
->stopped_by_vpn_controller
&& p
->parent_session
->is_asserted
) {
431 plog(ASL_LEVEL_NOTICE
,
432 "Skipping Phase 1 %s that's asserted...\n",
433 isakmp_pindex(&p
->index
, 0));
437 /* send delete information */
438 if (FSM_STATE_IS_ESTABLISHED(p
->status
)) {
439 if (ignore_estab_or_assert_handles
&&
440 (ike_session_has_negoing_ph2(p
->parent_session
) || ike_session_has_established_ph2(p
->parent_session
))) {
441 plog(ASL_LEVEL_NOTICE
,
442 "Skipping Phase 1 %s that's established... because it's needed by children Phase 2s\n",
443 isakmp_pindex(&p
->index
, 0));
446 /* send delete information */
447 plog(ASL_LEVEL_NOTICE
,
448 "Got a Phase 1 %s to flush...\n",
449 isakmp_pindex(&p
->index
, 0));
450 isakmp_info_send_d1(p
);
453 ike_session_stopped_by_controller(p
->parent_session
,
454 ike_session_stopped_by_flush
);
456 ike_session_unlink_phase1(p
);
464 ike_session_flush_all_phase1(int ignore_estab_or_assert_handles
)
466 ike_session_t
*session
= NULL
;
467 ike_session_t
*next_session
= NULL
;
469 plog(ASL_LEVEL_NOTICE
,
470 "Flushing Phase 1 handles: ignore_estab_or_assert %d...\n", ignore_estab_or_assert_handles
);
472 LIST_FOREACH_SAFE(session
, &ike_session_tree
, chain
, next_session
) {
473 ike_session_flush_all_phase1_for_session(session
, ignore_estab_or_assert_handles
);
480 * search ph2handle with policy id.
483 ike_session_getph2byspid(u_int32_t spid
)
485 ike_session_t
*session
= NULL
;
488 LIST_FOREACH(session
, &ike_session_tree
, chain
) {
489 LIST_FOREACH(p
, &session
->ph2tree
, ph2ofsession_chain
) {
491 * there are ph2handle independent on policy
492 * such like informational exchange.
504 * search ph2handle with sequence number.
505 * Used by PF_KEY functions to locate the phase2
508 ike_session_getph2byseq(u_int32_t seq
)
510 ike_session_t
*session
;
513 LIST_FOREACH(session
, &ike_session_tree
, chain
) {
514 LIST_FOREACH(p
, &session
->ph2tree
, ph2ofsession_chain
) {
523 * search ph2handle with message id.
526 ike_session_getph2bymsgid(phase1_handle_t
*iph1
, u_int32_t msgid
)
530 LIST_FOREACH(p
, &iph1
->parent_session
->ph2tree
, ph2ofsession_chain
) {
531 if (p
->msgid
== msgid
&& !p
->is_defunct
)
539 ike_session_getonlyph2(phase1_handle_t
*iph1
)
541 phase2_handle_t
*only_ph2
= NULL
;
542 phase2_handle_t
*p
= NULL
;
544 LIST_FOREACH(p
, &iph1
->bound_ph2tree
, ph2ofsession_chain
) {
545 if (only_ph2
) return NULL
;
553 ike_session_getph2byid(struct sockaddr_storage
*src
, struct sockaddr_storage
*dst
, u_int32_t spid
)
555 ike_session_t
*session
= NULL
;
556 ike_session_t
*next_session
= NULL
;
558 phase2_handle_t
*next_iph2
;
560 LIST_FOREACH_SAFE(session
, &ike_session_tree
, chain
, next_session
) {
561 LIST_FOREACH_SAFE(p
, &session
->ph2tree
, ph2ofsession_chain
, next_iph2
) {
562 if (spid
== p
->spid
&&
563 CMPSADDR(src
, p
->src
) == 0 &&
564 CMPSADDR(dst
, p
->dst
) == 0){
565 /* Sanity check to detect zombie handlers
566 * XXX Sould be done "somewhere" more interesting,
567 * because we have lots of getph2byxxxx(), but this one
568 * is called by pk_recvacquire(), so is the most important.
570 if(!FSM_STATE_IS_ESTABLISHED_OR_EXPIRED(p
->status
) &&
571 p
->retry_counter
== 0
572 && p
->sce
== 0 && p
->scr
== 0 &&
573 p
->retry_checkph1
== 0){
574 plog(ASL_LEVEL_NOTICE
,
575 "Zombie ph2 found, expiring it\n");
588 ike_session_getph2bysaddr(struct sockaddr_storage
*src
, struct sockaddr_storage
*dst
)
590 ike_session_t
*session
;
593 LIST_FOREACH(session
, &ike_session_tree
, chain
) {
594 LIST_FOREACH(p
, &session
->ph2tree
, chain
) {
595 if (cmpsaddrstrict(src
, p
->src
) == 0 &&
596 cmpsaddrstrict(dst
, p
->dst
) == 0)
603 #endif /* NOT_USED */
606 * call by pk_recvexpire().
609 ike_session_getph2bysaidx(struct sockaddr_storage
*src
, struct sockaddr_storage
*dst
, u_int proto_id
, u_int32_t spi
)
611 ike_session_t
*session
;
612 phase2_handle_t
*iph2
;
615 LIST_FOREACH(session
, &ike_session_tree
, chain
) {
616 LIST_FOREACH(iph2
, &session
->ph2tree
, ph2ofsession_chain
) {
617 if (iph2
->proposal
== NULL
&& iph2
->approval
== NULL
)
619 if (iph2
->approval
!= NULL
) {
620 for (pr
= iph2
->approval
->head
; pr
!= NULL
;
622 if (proto_id
!= pr
->proto_id
)
624 if (spi
== pr
->spi
|| spi
== pr
->spi_p
)
627 } else if (iph2
->proposal
!= NULL
) {
628 for (pr
= iph2
->proposal
->head
; pr
!= NULL
;
630 if (proto_id
!= pr
->proto_id
)
643 ike_session_getph2bysaidx2(struct sockaddr_storage
*src
, struct sockaddr_storage
*dst
, u_int proto_id
, u_int32_t spi
, u_int32_t
*opposite_spi
)
645 ike_session_t
*session
;
646 phase2_handle_t
*iph2
;
649 LIST_FOREACH(session
, &ike_session_tree
, chain
) {
650 LIST_FOREACH(iph2
, &session
->ph2tree
, ph2ofsession_chain
) {
651 if (iph2
->proposal
== NULL
&& iph2
->approval
== NULL
)
653 if (iph2
->approval
!= NULL
) {
654 for (pr
= iph2
->approval
->head
; pr
!= NULL
;
656 if (proto_id
!= pr
->proto_id
)
658 if (spi
== pr
->spi
|| spi
== pr
->spi_p
) {
660 *opposite_spi
= (spi
== pr
->spi
)? pr
->spi_p
: pr
->spi
;
665 } else if (iph2
->proposal
!= NULL
) {
666 for (pr
= iph2
->proposal
->head
; pr
!= NULL
;
668 if (proto_id
!= pr
->proto_id
)
670 if (spi
== pr
->spi
|| spi
== pr
->spi_p
) {
672 *opposite_spi
= (spi
== pr
->spi
)? pr
->spi_p
: pr
->spi
;
685 * create new isakmp Phase 2 status record to handle isakmp in Phase2
688 ike_session_newph2(unsigned int version
, int type
)
690 phase2_handle_t
*iph2
= NULL
;
692 /* create new iph2 */
693 iph2
= racoon_calloc(1, sizeof(*iph2
));
696 iph2
->version
= version
;
697 iph2
->phase2_type
= type
;
700 plog(ASL_LEVEL_NOTICE
, "New Phase 2\n");
705 * initialize ph2handle
706 * NOTE: don't initialize src/dst.
707 * SPI in the proposal is cleared.
710 ike_session_initph2(phase2_handle_t
*iph2
)
712 sched_scrub_param(iph2
);
716 VPTRINIT(iph2
->sendbuf
);
717 VPTRINIT(iph2
->msg1
);
719 /* clear spi, keep variables in the proposal */
720 if (iph2
->proposal
) {
722 for (pr
= iph2
->proposal
->head
; pr
!= NULL
; pr
= pr
->next
)
727 if (iph2
->approval
) {
728 flushsaprop(iph2
->approval
);
729 iph2
->approval
= NULL
;
732 /* clear the generated policy */
733 if (iph2
->spidx_gen
) {
734 delsp_bothdir(iph2
->spidx_gen
);
735 racoon_free(iph2
->spidx_gen
);
736 iph2
->spidx_gen
= NULL
;
740 oakley_dhgrp_free(iph2
->pfsgrp
);
744 VPTRINIT(iph2
->dhpriv
);
745 VPTRINIT(iph2
->dhpub
);
746 VPTRINIT(iph2
->dhpub_p
);
747 VPTRINIT(iph2
->dhgxy
);
749 VPTRINIT(iph2
->id_p
);
750 VPTRINIT(iph2
->nonce
);
751 VPTRINIT(iph2
->nonce_p
);
753 VPTRINIT(iph2
->sa_ret
);
756 oakley_delivm(iph2
->ivm
);
762 * delete new isakmp Phase 2 status record to handle isakmp in Phase2
765 ike_session_delph2(phase2_handle_t
*iph2
)
767 ike_session_initph2(iph2
);
770 racoon_free(iph2
->src
);
774 racoon_free(iph2
->dst
);
778 racoon_free(iph2
->src_id
);
782 racoon_free(iph2
->dst_id
);
786 if (iph2
->proposal
) {
787 flushsaprop(iph2
->proposal
);
788 iph2
->proposal
= NULL
;
792 release_sainfo(iph2
->sainfo
);
796 VPTRINIT(iph2
->id_p
);
797 VPTRINIT(iph2
->ext_nat_id
);
798 VPTRINIT(iph2
->ext_nat_id_p
);
801 SCHED_KILL(iph2
->sce
);
803 SCHED_KILL(iph2
->scr
);
809 ike_session_flush_all_phase2_for_session(ike_session_t
*session
, int ignore_estab_or_assert_handles
)
811 phase2_handle_t
*p
= NULL
;
812 phase2_handle_t
*next
= NULL
;
813 LIST_FOREACH_SAFE(p
, &session
->ph2tree
, ph2ofsession_chain
, next
) {
814 if (p
->is_dying
|| FSM_STATE_IS_EXPIRED(p
->status
)) {
817 if (ignore_estab_or_assert_handles
&& p
->parent_session
&& !p
->parent_session
->stopped_by_vpn_controller
&& p
->parent_session
->is_asserted
) {
818 plog(ASL_LEVEL_NOTICE
,
819 "skipping phase2 handle that's asserted...\n");
822 if (FSM_STATE_IS_ESTABLISHED(p
->status
)){
823 if (ignore_estab_or_assert_handles
) {
824 plog(ASL_LEVEL_NOTICE
,
825 "skipping ph2 handler that's established...\n");
828 /* send delete information */
829 plog(ASL_LEVEL_NOTICE
,
830 "got an established ph2 handler to flush...\n");
831 isakmp_info_send_d2(p
);
833 plog(ASL_LEVEL_NOTICE
,
834 "got a ph2 handler to flush (state %d)\n", p
->status
);
837 ike_session_stopped_by_controller(p
->parent_session
,
838 ike_session_stopped_by_flush
);
840 ike_session_unlink_phase2(p
);
845 ike_session_flush_all_phase2(int ignore_estab_or_assert_handles
)
847 ike_session_t
*session
= NULL
;
848 ike_session_t
*next_session
= NULL
;
850 plog(ASL_LEVEL_NOTICE
,
851 "flushing ph2 handles: ignore_estab_or_assert %d...\n", ignore_estab_or_assert_handles
);
853 LIST_FOREACH_SAFE(session
, &ike_session_tree
, chain
, next_session
) {
854 ike_session_flush_all_phase2_for_session(session
, ignore_estab_or_assert_handles
);
859 * Delete all Phase 2 handlers for this src/dst/proto. This
860 * is used during INITIAL-CONTACT processing (so no need to
861 * send a message to the peer).
863 //%%%%%%%%%%%%%%%%%%% make this smarter - find session using addresses ????
865 ike_session_deleteallph2(struct sockaddr_storage
*src
, struct sockaddr_storage
*dst
, u_int proto_id
)
867 ike_session_t
*session
= NULL
;
868 ike_session_t
*next_session
= NULL
;
869 phase2_handle_t
*iph2
= NULL
;
870 phase2_handle_t
*next_iph2
= NULL
;
873 LIST_FOREACH_SAFE(session
, &ike_session_tree
, chain
, next_session
) {
874 LIST_FOREACH_SAFE(iph2
, &session
->ph2tree
, ph2ofsession_chain
, next_iph2
) {
875 if (iph2
->is_dying
|| FSM_STATE_IS_EXPIRED(iph2
->status
)) {
878 if (iph2
->proposal
== NULL
&& iph2
->approval
== NULL
)
880 if (cmpsaddrwop(src
, iph2
->src
) != 0 ||
881 cmpsaddrwop(dst
, iph2
->dst
) != 0) {
884 if (iph2
->approval
!= NULL
) {
885 for (pr
= iph2
->approval
->head
; pr
!= NULL
;
887 if (proto_id
== pr
->proto_id
)
890 } else if (iph2
->proposal
!= NULL
) {
891 for (pr
= iph2
->proposal
->head
; pr
!= NULL
;
893 if (proto_id
== pr
->proto_id
)
899 plog(ASL_LEVEL_NOTICE
,
900 "deleteallph2: got a ph2 handler...\n");
901 if (FSM_STATE_IS_ESTABLISHED(iph2
->status
))
902 isakmp_info_send_d2(iph2
);
903 ike_session_stopped_by_controller(iph2
->parent_session
,
904 ike_session_stopped_by_flush
);
905 ike_session_unlink_phase2(iph2
);
911 * Delete all Phase 1 handlers for this src/dst.
914 ike_session_deleteallph1(struct sockaddr_storage
*src
, struct sockaddr_storage
*dst
)
916 ike_session_t
*session
= NULL
;
917 ike_session_t
*next_session
= NULL
;
918 phase1_handle_t
*iph1
= NULL
;
919 phase1_handle_t
*next_iph1
= NULL
;
921 LIST_FOREACH_SAFE(session
, &ike_session_tree
, chain
, next_session
) {
922 LIST_FOREACH_SAFE(iph1
, &session
->ph1tree
, ph1ofsession_chain
, next_iph1
) {
923 if (cmpsaddrwop(src
, iph1
->local
) != 0 ||
924 cmpsaddrwop(dst
, iph1
->remote
) != 0) {
927 plog(ASL_LEVEL_NOTICE
,
928 "deleteallph1: got a ph1 handler...\n");
929 if (FSM_STATE_IS_ESTABLISHED(iph1
->status
))
930 isakmp_info_send_d1(iph1
);
932 ike_session_stopped_by_controller(iph1
->parent_session
, ike_session_stopped_by_flush
);
933 ike_session_unlink_phase1(iph1
);
939 /* %%% management contacted list */
941 * search contacted list.
944 ike_session_getcontacted(remote
)
945 struct sockaddr_storage
*remote
;
949 LIST_FOREACH(p
, &ctdtree
, chain
) {
950 if (cmpsaddrstrict(remote
, p
->remote
) == 0)
958 * create new isakmp Phase 2 status record to handle isakmp in Phase2
961 ike_session_inscontacted(remote
)
962 struct sockaddr_storage
*remote
;
964 struct contacted
*new;
966 /* create new iph2 */
967 new = racoon_calloc(1, sizeof(*new));
971 new->remote
= dupsaddr(remote
);
972 if (new->remote
== NULL
) {
974 "failed to allocate buffer.\n");
979 LIST_INSERT_HEAD(&ctdtree
, new, chain
);
986 ike_session_clear_contacted()
988 struct contacted
*c
, *next
;
989 LIST_FOREACH_SAFE(c
, &ctdtree
, chain
, next
) {
990 LIST_REMOVE(c
, chain
);
991 racoon_free(c
->remote
);
997 ike_session_initctdtree()
1003 ike_session_get_exp_retx_interval (int num_retries
, int fixed_retry_interval
)
1005 // first 3 retries aren't exponential
1006 if (num_retries
<= 3) {
1007 return (time_t)fixed_retry_interval
;
1009 return (time_t)(num_retries
* fixed_retry_interval
);
1014 * check the response has been sent to the peer. when not, simply reply
1015 * the buffered packet to the peer.
1017 * 0: the packet is received at the first time.
1018 * 1: the packet was processed before.
1019 * 2: the packet was processed before, but the address mismatches.
1020 * -1: error happened.
1023 ike_session_check_recvdpkt(remote
, local
, rbuf
)
1024 struct sockaddr_storage
*remote
, *local
;
1032 /* set current time */
1035 hash
= eay_md5_one(rbuf
);
1038 "failed to allocate buffer.\n");
1042 LIST_FOREACH(r
, &rcptree
, chain
) {
1043 if (memcmp(hash
->v
, r
->hash
->v
, r
->hash
->l
) == 0)
1048 /* this is the first time to receive the packet */
1053 * the packet was processed before, but the remote address mismatches.
1054 * ignore the port to accomodate port changes (e.g. floating).
1056 if (cmpsaddrwop(remote
, r
->remote
) != 0) {
1061 * it should not check the local address because the packet
1062 * may arrive at other interface.
1065 /* check the previous time to send */
1066 if (t
- r
->time_send
< 1) {
1067 plog(ASL_LEVEL_WARNING
,
1068 "the packet retransmitted in a short time from %s\n",
1069 saddr2str((struct sockaddr
*)remote
));
1070 /*XXX should it be error ? */
1073 /* select the socket to be sent */
1074 s
= getsockmyaddr((struct sockaddr
*)r
->local
);
1078 // don't send if we recently sent a response.
1079 if (r
->time_send
&& t
> r
->time_send
) {
1080 d
= t
- r
->time_send
;
1081 if (d
< r
->retry_interval
) {
1082 plog(ASL_LEVEL_ERR
, "already responded within the past %ld secs\n", d
);
1088 if (r
->frag_flags
&& r
->sendbuf
->l
> ISAKMP_FRAG_MAXLEN
) {
1089 /* resend the packet if needed */
1090 plog(ASL_LEVEL_ERR
, "!!! retransmitting frags\n");
1091 len
= sendfragsfromto(s
, r
->sendbuf
,
1092 r
->local
, r
->remote
, lcconf
->count_persend
,
1095 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
);
1096 /* resend the packet if needed */
1097 len
= sendfromto(s
, r
->sendbuf
->v
, r
->sendbuf
->l
,
1098 r
->local
, r
->remote
, lcconf
->count_persend
);
1101 /* resend the packet if needed */
1102 len
= sendfromto(s
, r
->sendbuf
->v
, r
->sendbuf
->l
,
1103 r
->local
, r
->remote
, lcconf
->count_persend
);
1106 plog(ASL_LEVEL_ERR
, "sendfromto failed\n");
1110 /* check the retry counter */
1112 if (r
->retry_counter
<= 0) {
1113 ike_session_rem_recvdpkt(r
);
1114 ike_session_del_recvdpkt(r
);
1115 plog(ASL_LEVEL_NOTICE
,
1116 "deleted the retransmission packet to %s.\n",
1117 saddr2str((struct sockaddr
*)remote
));
1120 r
->retry_interval
= ike_session_get_exp_retx_interval((lcconf
->retry_counter
- r
->retry_counter
),
1121 lcconf
->retry_interval
);
1128 * adding a hash of received packet into the received list.
1131 ike_session_add_recvdpkt(remote
, local
, sbuf
, rbuf
, non_esp
, frag_flags
)
1132 struct sockaddr_storage
*remote
, *local
;
1133 vchar_t
*sbuf
, *rbuf
;
1135 u_int32_t frag_flags
;
1137 struct recvdpkt
*new = NULL
;
1139 if (lcconf
->retry_counter
== 0) {
1140 /* no need to add it */
1144 new = racoon_calloc(1, sizeof(*new));
1147 "failed to allocate buffer.\n");
1151 new->hash
= eay_md5_one(rbuf
);
1154 "failed to allocate buffer.\n");
1155 ike_session_del_recvdpkt(new);
1158 new->remote
= dupsaddr(remote
);
1159 if (new->remote
== NULL
) {
1161 "failed to allocate buffer.\n");
1162 ike_session_del_recvdpkt(new);
1165 new->local
= dupsaddr(local
);
1166 if (new->local
== NULL
) {
1168 "failed to allocate buffer.\n");
1169 ike_session_del_recvdpkt(new);
1174 plog (ASL_LEVEL_DEBUG
, "Adding NON-ESP marker\n");
1176 /* If NAT-T port floating is in use, 4 zero bytes (non-ESP marker)
1177 must added just before the packet itself. For this we must
1178 allocate a new buffer and release it at the end. */
1179 if ((new->sendbuf
= vmalloc (sbuf
->l
+ non_esp
)) == NULL
) {
1181 "failed to allocate extra buf for non-esp\n");
1182 ike_session_del_recvdpkt(new);
1185 *ALIGNED_CAST(u_int32_t
*)new->sendbuf
->v
= 0;
1186 memcpy(new->sendbuf
->v
+ non_esp
, sbuf
->v
, sbuf
->l
);
1188 new->sendbuf
= vdup(sbuf
);
1189 if (new->sendbuf
== NULL
) {
1191 "failed to allocate buffer.\n");
1192 ike_session_del_recvdpkt(new);
1197 new->retry_counter
= lcconf
->retry_counter
;
1199 new->created
= time(NULL
);
1202 new->frag_flags
= frag_flags
;
1205 new->retry_interval
= ike_session_get_exp_retx_interval((lcconf
->retry_counter
- new->retry_counter
),
1206 lcconf
->retry_interval
);
1208 LIST_INSERT_HEAD(&rcptree
, new, chain
);
1214 ike_session_del_recvdpkt(r
)
1218 racoon_free(r
->remote
);
1220 racoon_free(r
->local
);
1229 ike_session_rem_recvdpkt(r
)
1232 LIST_REMOVE(r
, chain
);
1236 sweep_recvdpkt(dummy
)
1239 struct recvdpkt
*r
, *next
;
1242 /* set current time */
1245 /* set the lifetime of the retransmission */
1246 lt
= lcconf
->retry_counter
* lcconf
->retry_interval
;
1248 LIST_FOREACH_SAFE(r
, &rcptree
, chain
, next
) {
1249 if (t
- r
->created
> lt
) {
1250 ike_session_rem_recvdpkt(r
);
1251 ike_session_del_recvdpkt(r
);
1255 sched_new(lt
, sweep_recvdpkt
, &rcptree
);
1259 ike_session_clear_recvdpkt()
1261 struct recvdpkt
*r
, *next
;
1263 LIST_FOREACH_SAFE(r
, &rcptree
, chain
, next
) {
1264 ike_session_rem_recvdpkt(r
);
1265 ike_session_del_recvdpkt(r
);
1267 sched_scrub_param(&rcptree
);
1271 ike_session_init_recvdpkt()
1273 time_t lt
= lcconf
->retry_counter
* lcconf
->retry_interval
;
1275 LIST_INIT(&rcptree
);
1277 sched_new(lt
, sweep_recvdpkt
, &rcptree
);
1281 #ifdef ENABLE_HYBRID
1283 * Returns 0 if the address was obtained by ISAKMP mode config, 1 otherwise
1284 * This should be in isakmp_cfg.c but ph1tree being private, it must be there
1287 exclude_cfg_addr(const struct sockaddr_storage
*addr
)
1289 ike_session_t
*session
;
1291 struct sockaddr_in
*sin
;
1293 LIST_FOREACH(session
, &ike_session_tree
, chain
) {
1294 LIST_FOREACH(p
, &session
->ph1tree
, chain
) {
1295 if ((p
->mode_cfg
!= NULL
) &&
1296 (p
->mode_cfg
->flags
& ISAKMP_CFG_GOT_ADDR4
) &&
1297 (addr
->ss_family
== AF_INET
)) {
1298 sin
= (struct sockaddr_in
*)addr
;
1299 if (sin
->sin_addr
.s_addr
== p
->mode_cfg
->addr4
.s_addr
)
1308 #endif /* NOT_USED */
1311 ike_session_expire_session(ike_session_t
*session
)
1315 phase1_handle_t
*next
;
1316 phase2_handle_t
*p2
;
1318 if (session
== NULL
)
1321 LIST_FOREACH(p2
, &session
->ph2tree
, ph2ofsession_chain
) {
1322 if (p2
->is_dying
|| FSM_STATE_IS_EXPIRED(p2
->status
)) {
1326 // Don't send a delete, since the ph1 implies the removal of ph2s
1327 isakmp_ph2expire(p2
);
1331 LIST_FOREACH_SAFE(p
, &session
->ph1tree
, ph1ofsession_chain
, next
) {
1332 if (p
->is_dying
|| FSM_STATE_IS_EXPIRED(p
->status
)) {
1336 ike_session_purge_ph2s_by_ph1(p
);
1337 if (FSM_STATE_IS_ESTABLISHED(p
->status
))
1338 isakmp_info_send_d1(p
);
1339 isakmp_ph1expire(p
);
1346 #ifdef ENABLE_HYBRID
1348 ike_session_purgephXbydstaddrwop(struct sockaddr_storage
*remote
)
1351 ike_session_t
*session
= NULL
;
1352 ike_session_t
*next_session
= NULL
;
1354 phase2_handle_t
*p2
;
1356 LIST_FOREACH_SAFE(session
, &ike_session_tree
, chain
, next_session
) {
1357 LIST_FOREACH(p2
, &session
->ph2tree
, ph2ofsession_chain
) {
1358 if (p2
->is_dying
|| FSM_STATE_IS_EXPIRED(p2
->status
)) {
1361 if (cmpsaddrwop(remote
, p2
->dst
) == 0) {
1362 plog(ASL_LEVEL_NOTICE
,
1363 "in %s... purging Phase 2 structures\n", __FUNCTION__
);
1364 if (FSM_STATE_IS_ESTABLISHED(p2
->status
))
1365 isakmp_info_send_d2(p2
);
1366 isakmp_ph2expire(p2
);
1371 LIST_FOREACH(p
, &session
->ph1tree
, ph1ofsession_chain
) {
1372 if (p
->is_dying
|| FSM_STATE_IS_EXPIRED(p
->status
)) {
1375 if (cmpsaddrwop(remote
, p
->remote
) == 0) {
1376 plog(ASL_LEVEL_NOTICE
,
1377 "in %s... purging Phase 1 and related Phase 2 structures\n", __FUNCTION__
);
1378 ike_session_purge_ph2s_by_ph1(p
);
1379 if (FSM_STATE_IS_ESTABLISHED(p
->status
))
1380 isakmp_info_send_d1(p
);
1381 isakmp_ph1expire(p
);
1391 ike_session_purgephXbyspid(u_int32_t spid
, int del_boundph1
)
1393 ike_session_t
*session
= NULL
;
1394 ike_session_t
*next_session
= NULL
;
1395 phase2_handle_t
*iph2
= NULL
;
1396 phase2_handle_t
*next_iph2
= NULL
;
1397 phase1_handle_t
*iph1
= NULL
;
1398 phase1_handle_t
*next_iph1
= NULL
;
1400 LIST_FOREACH_SAFE(session
, &ike_session_tree
, chain
, next_session
) {
1401 // do ph2's first... we need the ph1s for notifications
1402 LIST_FOREACH_SAFE(iph2
, &session
->ph2tree
, ph2ofsession_chain
, next_iph2
) {
1403 if (spid
== iph2
->spid
) {
1404 if (iph2
->is_dying
|| FSM_STATE_IS_EXPIRED(iph2
->status
)) {
1407 if (FSM_STATE_IS_ESTABLISHED(iph2
->status
)) {
1408 isakmp_info_send_d2(iph2
);
1410 ike_session_stopped_by_controller(iph2
->parent_session
,
1411 ike_session_stopped_by_flush
);
1412 isakmp_ph2expire(iph2
); // iph2 will go down 1 second later.
1416 // do the ph1s last. %%%%%%%%%%%%%%%%%% re-organize this - check del_boundph1 first
1417 LIST_FOREACH_SAFE(iph2
, &session
->ph2tree
, ph2ofsession_chain
, next_iph2
) {
1418 if (spid
== iph2
->spid
) {
1419 if (del_boundph1
&& iph2
->parent_session
) {
1420 LIST_FOREACH_SAFE(iph1
, &iph2
->parent_session
->ph1tree
, ph1ofsession_chain
, next_iph1
) {
1421 if (iph1
->is_dying
|| FSM_STATE_IS_EXPIRED(iph1
->status
)) {
1424 if (FSM_STATE_IS_ESTABLISHED(iph1
->status
)) {
1425 isakmp_info_send_d1(iph1
);
1427 isakmp_ph1expire(iph1
);
1439 ike_session_ph1_force_dpd (struct sockaddr_storage
*remote
)
1442 ike_session_t
*session
= NULL
;
1443 phase1_handle_t
*p
= NULL
;
1445 LIST_FOREACH(session
, &ike_session_tree
, chain
) {
1446 LIST_FOREACH(p
, &session
->ph1tree
, ph1ofsession_chain
) {
1447 if (cmpsaddrwop(remote
, p
->remote
) == 0) {
1448 if (FSM_STATE_IS_ESTABLISHED(p
->status
) &&
1451 p
->rmconf
->dpd_interval
) {
1453 isakmp_info_send_r_u(p
);
1456 plog(ASL_LEVEL_NOTICE
, "Skipping forced-DPD for Phase 1 (dpd already in progress).\n");
1458 if (p
->parent_session
) {
1459 p
->parent_session
->controller_awaiting_peer_resp
= 1;
1462 plog(ASL_LEVEL_NOTICE
, "Skipping forced-DPD for Phase 1 (status %d, dying %d, dpd-support %d, dpd-interval %d).\n",
1463 p
->status
, p
->is_dying
, p
->dpd_support
, p
->rmconf
->dpd_interval
);
1474 sweep_sleepwake(void)
1476 ike_session_t
*session
= NULL
;
1477 ike_session_t
*next_session
= NULL
;
1478 phase2_handle_t
*iph2
= NULL
;
1479 phase2_handle_t
*next_iph2
= NULL
;
1480 phase1_handle_t
*iph1
= NULL
;
1481 phase1_handle_t
*next_iph1
= NULL
;
1483 LIST_FOREACH_SAFE(session
, &ike_session_tree
, chain
, next_session
) {
1485 LIST_FOREACH_SAFE(iph1
, &session
->ph1tree
, ph1ofsession_chain
, next_iph1
) {
1486 if (iph1
->parent_session
&& iph1
->parent_session
->is_asserted
) {
1487 plog(ASL_LEVEL_NOTICE
, "Skipping sweep of Phase 1 %s because it's been asserted.\n",
1488 isakmp_pindex(&iph1
->index
, 0));
1491 if (iph1
->is_dying
|| FSM_STATE_IS_EXPIRED(iph1
->status
)) {
1492 plog(ASL_LEVEL_NOTICE
, "Skipping sweep of Phase 1 %s because it's already expired.\n",
1493 isakmp_pindex(&iph1
->index
, 0));
1498 if (sched_get_time(iph1
->sce
, &xtime
)) {
1499 if (xtime
<= swept_at
) {
1500 SCHED_KILL(iph1
->sce
);
1501 SCHED_KILL(iph1
->sce_rekey
);
1503 fsm_set_state(&iph1
->status
, IKEV1_STATE_PHASE1_EXPIRED
);
1504 ike_session_update_ph1_ph2tree(iph1
); // move unbind/rebind ph2s to from current ph1
1505 iph1
->sce
= sched_new(1, isakmp_ph1delete_stub
, iph1
);
1506 plog(ASL_LEVEL_NOTICE
, "Phase 1 %s expired while sleeping: quick deletion.\n",
1507 isakmp_pindex(&iph1
->index
, 0));
1511 if (iph1
->sce_rekey
) {
1513 if (sched_get_time(iph1
->sce_rekey
, &xtime
)) {
1514 if (FSM_STATE_IS_EXPIRED(iph1
->status
) || xtime
<= swept_at
) {
1515 SCHED_KILL(iph1
->sce_rekey
);
1521 if (sched_get_time(iph1
->scr
, &xtime
)) {
1522 if (FSM_STATE_IS_EXPIRED(iph1
->status
) || xtime
<= swept_at
) {
1523 SCHED_KILL(iph1
->scr
);
1528 if (iph1
->dpd_r_u
) {
1530 if (sched_get_time(iph1
->dpd_r_u
, &xtime
)) {
1531 if (FSM_STATE_IS_EXPIRED(iph1
->status
) || xtime
<= swept_at
) {
1532 SCHED_KILL(iph1
->dpd_r_u
);
1540 LIST_FOREACH_SAFE(iph2
, &session
->ph2tree
, ph2ofsession_chain
, next_iph2
) {
1541 if (iph2
->parent_session
&& iph2
->parent_session
->is_asserted
) {
1542 plog(ASL_LEVEL_NOTICE
, "Skipping sweep of Phase 2 because it's been asserted.\n");
1545 if (iph2
->is_dying
|| FSM_STATE_IS_EXPIRED(iph2
->status
)) {
1546 plog(ASL_LEVEL_NOTICE
, "Skipping sweep of Phase 2 because it's already expired.\n");
1551 if (sched_get_time(iph2
->sce
, &xtime
)) {
1552 if (xtime
<= swept_at
) {
1553 fsm_set_state(&iph2
->status
, IKEV1_STATE_PHASE2_EXPIRED
);
1555 isakmp_ph2expire(iph2
); // iph2 will go down 1 second later.
1556 ike_session_stopped_by_controller(iph2
->parent_session
,
1557 ike_session_stopped_by_sleepwake
);
1558 plog(ASL_LEVEL_NOTICE
, "Phase 2 expired while sleeping: quick deletion.\n");
1564 if (sched_get_time(iph2
->scr
, &xtime
)) {
1565 if (FSM_STATE_IS_EXPIRED(iph2
->status
) || xtime
<= swept_at
) {
1566 SCHED_KILL(iph2
->scr
);
1572 //%%%%%%%%%%%%%%% fix this
1573 // do the ike_session last
1574 ike_session_sweep_sleepwake();
1578 iph1_get_remote_v4_address(phase1_handle_t
*iph1
)
1580 uint32_t address
= 0;
1581 if (iph1
->remote
->ss_family
== AF_INET
) {
1582 address
= ((struct sockaddr_in
*)iph1
->remote
)->sin_addr
.s_addr
;
1583 } else if (iph1
->remote
->ss_family
== AF_INET6
&&
1584 iph1
->nat64_prefix
.length
) {
1585 if (!nw_nat64_extract_v4(&iph1
->nat64_prefix
, &((struct sockaddr_in6
*)iph1
->remote
)->sin6_addr
, (struct in_addr
*)&address
)) {
1586 plog(ASL_LEVEL_ERR
, "Failed to extract IPv4 from Phase 1 IPv6 address.\n");
1589 plog(ASL_LEVEL_ERR
, "Failed to get IPv4 address for Phase 1 (family=%u, NAT64Prefix=%u)\n",
1590 iph1
->remote
->ss_family
,
1591 iph1
->nat64_prefix
.length
);
1597 iph2_get_remote_v4_address(phase2_handle_t
*iph2
)
1599 uint32_t address
= 0;
1600 if (iph2
->dst
->ss_family
== AF_INET
) {
1601 address
= ((struct sockaddr_in
*)iph2
->dst
)->sin_addr
.s_addr
;
1602 } else if (iph2
->dst
->ss_family
== AF_INET6
&&
1603 iph2
->nat64_prefix
.length
) {
1604 if (!nw_nat64_extract_v4(&iph2
->nat64_prefix
, &((struct sockaddr_in6
*)iph2
->dst
)->sin6_addr
, (struct in_addr
*)&address
)) {
1605 plog(ASL_LEVEL_ERR
, "Failed to extract IPv4 from Phase 2 IPv6 address.\n");
1608 plog(ASL_LEVEL_ERR
, "Failed to get IPv4 address for Phase 2 (family=%u, NAT64Prefix=%u)\n",
1609 iph2
->dst
->ss_family
,
1610 iph2
->nat64_prefix
.length
);