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>
58 #include "grabmyaddr.h"
59 #include "algorithm.h"
60 #include "crypto_openssl.h"
63 #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"
85 static LIST_HEAD(_ph1tree_
, ph1handle
) ph1tree
;
86 static LIST_HEAD(_ph2tree_
, ph2handle
) ph2tree
;
87 static LIST_HEAD(_ctdtree_
, contacted
) ctdtree
;
88 static LIST_HEAD(_rcptree_
, recvdpkt
) rcptree
;
90 static void del_recvdpkt
__P((struct recvdpkt
*));
91 static void rem_recvdpkt
__P((struct recvdpkt
*));
92 static void sweep_recvdpkt
__P((void *));
95 * functions about management of the isakmp status table
97 /* %%% management phase 1 handler */
99 * search for isakmpsa handler with isakmp index.
102 extern caddr_t
val2str(const char *, size_t);
110 LIST_FOREACH(p
, &ph1tree
, chain
) {
111 if (p
->status
== PHASE1ST_EXPIRED
)
113 if (memcmp(&p
->index
, index
, sizeof(*index
)) == 0)
121 * search for isakmp handler by i_ck in index.
124 getph1byindex0(index
)
129 LIST_FOREACH(p
, &ph1tree
, chain
) {
130 if (p
->status
== PHASE1ST_EXPIRED
)
132 if (memcmp(&p
->index
, index
, sizeof(cookie_t
)) == 0)
140 * search for isakmpsa handler by source and remote address.
141 * don't use port number to search because this function search
142 * with phase 2's destinaion.
145 getph1byaddr(local
, remote
)
146 struct sockaddr
*local
, *remote
;
150 plog(LLV_DEBUG2
, LOCATION
, NULL
, "getph1byaddr: start\n");
151 plog(LLV_DEBUG2
, LOCATION
, NULL
, "local: %s\n", saddr2str(local
));
152 plog(LLV_DEBUG2
, LOCATION
, NULL
, "remote: %s\n", saddr2str(remote
));
154 LIST_FOREACH(p
, &ph1tree
, chain
) {
155 if (p
->status
== PHASE1ST_EXPIRED
)
157 plog(LLV_DEBUG2
, LOCATION
, NULL
, "p->local: %s\n", saddr2str(p
->local
));
158 plog(LLV_DEBUG2
, LOCATION
, NULL
, "p->remote: %s\n", saddr2str(p
->remote
));
159 if (CMPSADDR(local
, p
->local
) == 0
160 && CMPSADDR(remote
, p
->remote
) == 0){
161 plog(LLV_DEBUG2
, LOCATION
, NULL
, "matched\n");
166 plog(LLV_DEBUG2
, LOCATION
, NULL
, "no match\n");
172 getph1byaddrwop(local
, remote
)
173 struct sockaddr
*local
, *remote
;
177 LIST_FOREACH(p
, &ph1tree
, chain
) {
178 if (p
->status
== PHASE1ST_EXPIRED
)
180 if (cmpsaddrwop(local
, p
->local
) == 0
181 && cmpsaddrwop(remote
, p
->remote
) == 0)
189 * search for isakmpsa handler by remote address.
190 * don't use port number to search because this function search
191 * with phase 2's destinaion.
194 getph1bydstaddrwop(remote
)
195 struct sockaddr
*remote
;
199 LIST_FOREACH(p
, &ph1tree
, chain
) {
200 if (p
->status
== PHASE1ST_EXPIRED
)
202 if (cmpsaddrwop(remote
, p
->remote
) == 0)
211 struct ph1handle
*ph1
;
215 LIST_FOREACH(p
, &ph1tree
, chain
) {
216 if (p
->status
== PHASE1ST_EXPIRED
)
218 if (CMPSADDR(ph1
->remote
, p
->remote
) == 0) {
233 struct ph1handle
*iph1
;
238 /* get length of buffer */
239 LIST_FOREACH(iph1
, &ph1tree
, chain
)
242 buf
= vmalloc(cnt
* sizeof(struct ph1dump
));
244 plog(LLV_ERROR
, LOCATION
, NULL
,
245 "failed to get buffer\n");
248 pd
= (struct ph1dump
*)buf
->v
;
250 LIST_FOREACH(iph1
, &ph1tree
, chain
) {
251 memcpy(&pd
->index
, &iph1
->index
, sizeof(iph1
->index
));
252 pd
->status
= iph1
->status
;
253 pd
->side
= iph1
->side
;
254 memcpy(&pd
->remote
, iph1
->remote
, sysdep_sa_len(iph1
->remote
));
255 memcpy(&pd
->local
, iph1
->local
, sysdep_sa_len(iph1
->local
));
256 pd
->version
= iph1
->version
;
257 pd
->etype
= iph1
->etype
;
258 pd
->created
= iph1
->created
;
259 pd
->ph2cnt
= iph1
->ph2cnt
;
267 * create new isakmp Phase 1 status record to handle isakmp in Phase1
272 struct ph1handle
*iph1
;
274 /* create new iph1 */
275 iph1
= racoon_calloc(1, sizeof(*iph1
));
279 iph1
->status
= PHASE1ST_SPAWN
;
282 iph1
->dpd_support
= 0;
283 iph1
->dpd_lastack
= 0;
286 iph1
->peer_sent_ike
= 0;
287 iph1
->dpd_r_u
= NULL
;
289 #ifdef ENABLE_VPNCONTROL_PORT
290 iph1
->ping_sched
= NULL
;
297 * delete new isakmp Phase 1 status record to handle isakmp in Phase1
301 struct ph1handle
*iph1
;
306 /* SA down shell script hook */
307 script_hook(iph1
, SCRIPT_PHASE1_DOWN
);
309 EVT_PUSH(iph1
->local
, iph1
->remote
, EVTT_PHASE1_DOWN
, NULL
);
313 if (iph1
->natt_flags
& NAT_KA_QUEUED
)
314 natt_keepalive_remove (iph1
->local
, iph1
->remote
);
315 #endif /* __APPLE__ */
316 if (iph1
->natt_options
) {
317 racoon_free(iph1
->natt_options
);
318 iph1
->natt_options
= NULL
;
324 isakmp_cfg_rmstate(iph1
);
325 VPTRINIT(iph1
->xauth_awaiting_userinput_msg
);
329 if (iph1
->dpd_r_u
!= NULL
)
330 SCHED_KILL(iph1
->dpd_r_u
);
332 #ifdef ENABLE_VPNCONTROL_PORT
333 if (iph1
->ping_sched
!= NULL
)
334 SCHED_KILL(iph1
->ping_sched
);
338 racoon_free(iph1
->remote
);
342 racoon_free(iph1
->local
);
346 if (iph1
->approval
) {
347 delisakmpsa(iph1
->approval
);
348 iph1
->approval
= NULL
;
351 VPTRINIT(iph1
->authstr
);
353 sched_scrub_param(iph1
);
355 iph1
->sce_rekey
= NULL
;
358 VPTRINIT(iph1
->sendbuf
);
360 VPTRINIT(iph1
->dhpriv
);
361 VPTRINIT(iph1
->dhpub
);
362 VPTRINIT(iph1
->dhpub_p
);
363 VPTRINIT(iph1
->dhgxy
);
364 VPTRINIT(iph1
->nonce
);
365 VPTRINIT(iph1
->nonce_p
);
366 VPTRINIT(iph1
->skeyid
);
367 VPTRINIT(iph1
->skeyid_d
);
368 VPTRINIT(iph1
->skeyid_a
);
369 VPTRINIT(iph1
->skeyid_e
);
371 VPTRINIT(iph1
->hash
);
373 VPTRINIT(iph1
->sig_p
);
374 oakley_delcert(iph1
->cert
);
376 oakley_delcert(iph1
->cert_p
);
378 oakley_delcert(iph1
->crl_p
);
380 oakley_delcert(iph1
->cr_p
);
383 VPTRINIT(iph1
->id_p
);
385 if(iph1
->approval
!= NULL
)
386 delisakmpsa(iph1
->approval
);
389 oakley_delivm(iph1
->ivm
);
394 VPTRINIT(iph1
->sa_ret
);
397 VPTRINIT(iph1
->gi_i
);
398 VPTRINIT(iph1
->gi_r
);
400 gssapi_free_state(iph1
);
404 if (iph1
->parent_session
) {
405 ike_session_unlink_ph1_from_session(iph1
);
408 unlink_rmconf_from_ph1(iph1
->rmconf
);
417 * create new isakmp Phase 1 status record to handle isakmp in Phase1
421 struct ph1handle
*iph1
;
424 if (iph1
->remote
== NULL
) {
425 plog(LLV_ERROR
, LOCATION
, NULL
,
426 "invalid isakmp SA handler. no remote address.\n");
429 LIST_INSERT_HEAD(&ph1tree
, iph1
, chain
);
436 struct ph1handle
*iph1
;
438 LIST_REMOVE(iph1
, chain
);
445 flushph1(int ignore_established_handles
)
447 struct ph1handle
*p
, *next
;
449 for (p
= LIST_FIRST(&ph1tree
); p
; p
= next
) {
450 next
= LIST_NEXT(p
, chain
);
452 /* send delete information */
453 if (p
->status
== PHASE1ST_ESTABLISHED
) {
454 if (ignore_established_handles
&&
455 (ike_session_has_negoing_ph2(p
->parent_session
) ||
456 p
->mode_cfg
->flags
)) {
457 plog(LLV_DEBUG2
, LOCATION
, NULL
,
458 "skipping ph1 handler that's established... because it's needed by children phase2s\n");
461 /* send delete information */
462 plog(LLV_DEBUG2
, LOCATION
, NULL
,
463 "got a ph1 handler to flush...\n");
464 isakmp_info_send_d1(p
);
467 ike_session_stopped_by_controller(p
->parent_session
,
468 ike_session_stopped_by_flush
);
480 /* %%% management phase 2 handler */
482 * search ph2handle with policy id.
490 LIST_FOREACH(p
, &ph2tree
, chain
) {
492 * there are ph2handle independent on policy
493 * such like informational exchange.
503 * search ph2handle with sequence number.
511 LIST_FOREACH(p
, &ph2tree
, chain
) {
520 * search ph2handle with message id.
523 getph2bymsgid(iph1
, msgid
)
524 struct ph1handle
*iph1
;
529 LIST_FOREACH(p
, &ph2tree
, chain
) {
530 if (p
->msgid
== msgid
)
538 getph2byid(src
, dst
, spid
)
539 struct sockaddr
*src
, *dst
;
544 LIST_FOREACH(p
, &ph2tree
, chain
) {
545 if (spid
== p
->spid
&&
546 CMPSADDR(src
, p
->src
) == 0 &&
547 CMPSADDR(dst
, p
->dst
) == 0){
548 /* Sanity check to detect zombie handlers
549 * XXX Sould be done "somewhere" more interesting,
550 * because we have lots of getph2byxxxx(), but this one
551 * is called by pk_recvacquire(), so is the most important.
553 if(p
->status
< PHASE2ST_ESTABLISHED
&&
554 p
->retry_counter
== 0
555 && p
->sce
== NULL
&& p
->scr
== NULL
){
556 plog(LLV_DEBUG
, LOCATION
, NULL
,
557 "Zombie ph2 found, expiring it\n");
568 getph2bysaddr(src
, dst
)
569 struct sockaddr
*src
, *dst
;
573 LIST_FOREACH(p
, &ph2tree
, chain
) {
574 if (cmpsaddrstrict(src
, p
->src
) == 0 &&
575 cmpsaddrstrict(dst
, p
->dst
) == 0)
583 * call by pk_recvexpire().
586 getph2bysaidx(src
, dst
, proto_id
, spi
)
587 struct sockaddr
*src
, *dst
;
591 struct ph2handle
*iph2
;
594 LIST_FOREACH(iph2
, &ph2tree
, chain
) {
595 if (iph2
->proposal
== NULL
&& iph2
->approval
== NULL
)
597 if (iph2
->approval
!= NULL
) {
598 for (pr
= iph2
->approval
->head
; pr
!= NULL
;
600 if (proto_id
!= pr
->proto_id
)
602 if (spi
== pr
->spi
|| spi
== pr
->spi_p
)
605 } else if (iph2
->proposal
!= NULL
) {
606 for (pr
= iph2
->proposal
->head
; pr
!= NULL
;
608 if (proto_id
!= pr
->proto_id
)
620 * create new isakmp Phase 2 status record to handle isakmp in Phase2
625 struct ph2handle
*iph2
= NULL
;
627 /* create new iph2 */
628 iph2
= racoon_calloc(1, sizeof(*iph2
));
632 iph2
->status
= PHASE1ST_SPAWN
;
639 * initialize ph2handle
640 * NOTE: don't initialize src/dst.
641 * SPI in the proposal is cleared.
645 struct ph2handle
*iph2
;
647 sched_scrub_param(iph2
);
651 VPTRINIT(iph2
->sendbuf
);
652 VPTRINIT(iph2
->msg1
);
654 /* clear spi, keep variables in the proposal */
655 if (iph2
->proposal
) {
657 for (pr
= iph2
->proposal
->head
; pr
!= NULL
; pr
= pr
->next
)
662 if (iph2
->approval
) {
663 flushsaprop(iph2
->approval
);
664 iph2
->approval
= NULL
;
667 /* clear the generated policy */
668 if (iph2
->spidx_gen
) {
669 delsp_bothdir((struct policyindex
*)iph2
->spidx_gen
);
670 racoon_free(iph2
->spidx_gen
);
671 iph2
->spidx_gen
= NULL
;
675 oakley_dhgrp_free(iph2
->pfsgrp
);
679 VPTRINIT(iph2
->dhpriv
);
680 VPTRINIT(iph2
->dhpub
);
681 VPTRINIT(iph2
->dhpub_p
);
682 VPTRINIT(iph2
->dhgxy
);
684 VPTRINIT(iph2
->id_p
);
685 VPTRINIT(iph2
->nonce
);
686 VPTRINIT(iph2
->nonce_p
);
688 VPTRINIT(iph2
->sa_ret
);
691 oakley_delivm(iph2
->ivm
);
697 * delete new isakmp Phase 2 status record to handle isakmp in Phase2
701 struct ph2handle
*iph2
;
706 racoon_free(iph2
->src
);
710 racoon_free(iph2
->dst
);
714 racoon_free(iph2
->src_id
);
718 racoon_free(iph2
->dst_id
);
722 if (iph2
->proposal
) {
723 flushsaprop(iph2
->proposal
);
724 iph2
->proposal
= NULL
;
728 if (iph2
->parent_session
) {
729 ike_session_unlink_ph2_from_session(iph2
);
732 unlink_sainfo_from_ph2(iph2
->sainfo
);
741 * create new isakmp Phase 2 status record to handle isakmp in Phase2
745 struct ph2handle
*iph2
;
747 LIST_INSERT_HEAD(&ph2tree
, iph2
, chain
);
754 struct ph2handle
*iph2
;
756 LIST_REMOVE(iph2
, chain
);
766 flushph2(int ignore_established_handles
)
768 struct ph2handle
*p
, *next
;
770 plog(LLV_DEBUG2
, LOCATION
, NULL
,
771 "flushing all ph2 handlers...\n");
773 for (p
= LIST_FIRST(&ph2tree
); p
; p
= next
) {
774 next
= LIST_NEXT(p
, chain
);
776 if (p
->status
== PHASE2ST_ESTABLISHED
){
777 if (ignore_established_handles
) {
778 plog(LLV_DEBUG2
, LOCATION
, NULL
,
779 "skipping ph2 handler that's established...\n");
782 /* send delete information */
783 plog(LLV_DEBUG2
, LOCATION
, NULL
,
784 "got an established ph2 handler to flush...\n");
785 isakmp_info_send_d2(p
);
787 plog(LLV_DEBUG2
, LOCATION
, NULL
,
788 "got a ph2 handler to flush (state %d)\n", p
->status
);
791 ike_session_stopped_by_controller(p
->parent_session
,
792 ike_session_stopped_by_flush
);
801 * Delete all Phase 2 handlers for this src/dst/proto. This
802 * is used during INITIAL-CONTACT processing (so no need to
803 * send a message to the peer).
806 deleteallph2(src
, dst
, proto_id
)
807 struct sockaddr
*src
, *dst
;
810 struct ph2handle
*iph2
, *next
;
813 for (iph2
= LIST_FIRST(&ph2tree
); iph2
!= NULL
; iph2
= next
) {
814 next
= LIST_NEXT(iph2
, chain
);
815 if (iph2
->proposal
== NULL
&& iph2
->approval
== NULL
)
817 if (cmpsaddrwop(src
, iph2
->src
) != 0 ||
818 cmpsaddrwop(dst
, iph2
->dst
) != 0) {
821 if (iph2
->approval
!= NULL
) {
822 for (pr
= iph2
->approval
->head
; pr
!= NULL
;
824 if (proto_id
== pr
->proto_id
)
827 } else if (iph2
->proposal
!= NULL
) {
828 for (pr
= iph2
->proposal
->head
; pr
!= NULL
;
830 if (proto_id
== pr
->proto_id
)
836 plog(LLV_DEBUG2
, LOCATION
, NULL
,
837 "deleteallph2: got a ph2 handler...\n");
838 if (iph2
->status
== PHASE2ST_ESTABLISHED
)
839 isakmp_info_send_d2(iph2
);
840 ike_session_stopped_by_controller(iph2
->parent_session
,
841 ike_session_stopped_by_flush
);
849 * Delete all Phase 1 handlers for this src/dst.
852 deleteallph1(src
, dst
)
853 struct sockaddr
*src
, *dst
;
855 struct ph1handle
*iph1
, *next
;
857 for (iph1
= LIST_FIRST(&ph1tree
); iph1
!= NULL
; iph1
= next
) {
858 next
= LIST_NEXT(iph1
, chain
);
859 if (cmpsaddrwop(src
, iph1
->local
) != 0 ||
860 cmpsaddrwop(dst
, iph1
->remote
) != 0) {
863 plog(LLV_DEBUG2
, LOCATION
, NULL
,
864 "deleteallph1: got a ph1 handler...\n");
865 if (iph1
->status
== PHASE2ST_ESTABLISHED
)
866 isakmp_info_send_d1(iph1
);
868 ike_session_stopped_by_controller(iph1
->parent_session
,
869 ike_session_stopped_by_flush
);
878 struct ph1handle
*iph1
;
879 struct ph2handle
*iph2
;
881 if (iph2
->ph1
&& (struct ph1handle
*)iph2
->ph1bind
.le_next
== iph1
) {
882 plog(LLV_ERROR
, LOCATION
, NULL
, "duplicate %s.\n", __FUNCTION__
);
885 LIST_INSERT_HEAD(&iph1
->ph2tree
, iph2
, ph1bind
);
890 struct ph2handle
*iph2
;
892 if (iph2
->ph1
!= NULL
) {
893 plog(LLV_DEBUG
, LOCATION
, NULL
, "unbindph12.\n");
895 LIST_REMOVE(iph2
, ph1bind
);
900 rebindph12(new_ph1
, iph2
)
901 struct ph1handle
*new_ph1
;
902 struct ph2handle
*iph2
;
908 // reconcile the ph1-to-ph2 binding
909 plog(LLV_DEBUG
, LOCATION
, NULL
, "rebindph12.\n");
911 bindph12(new_ph1
, iph2
);
912 // recalculate ivm since ph1 binding has changed
913 if (iph2
->ivm
!= NULL
) {
914 oakley_delivm(iph2
->ivm
);
915 if (new_ph1
->status
== PHASE1ST_ESTABLISHED
) {
916 iph2
->ivm
= oakley_newiv2(new_ph1
, iph2
->msgid
);
917 plog(LLV_DEBUG
, LOCATION
, NULL
, "ph12 binding changed... recalculated ivm.\n");
924 /* %%% management contacted list */
926 * search contacted list.
930 struct sockaddr
*remote
;
934 LIST_FOREACH(p
, &ctdtree
, chain
) {
935 if (cmpsaddrstrict(remote
, p
->remote
) == 0)
943 * create new isakmp Phase 2 status record to handle isakmp in Phase2
947 struct sockaddr
*remote
;
949 struct contacted
*new;
951 /* create new iph2 */
952 new = racoon_calloc(1, sizeof(*new));
956 new->remote
= dupsaddr(remote
);
957 if (new->remote
== NULL
) {
958 plog(LLV_ERROR
, LOCATION
, NULL
,
959 "failed to allocate buffer.\n");
964 LIST_INSERT_HEAD(&ctdtree
, new, chain
);
973 struct contacted
*c
, *next
;
975 for (c
= LIST_FIRST(&ctdtree
); c
; c
= next
) {
976 next
= LIST_NEXT(c
, chain
);
977 LIST_REMOVE(c
, chain
);
978 racoon_free(c
->remote
);
990 * check the response has been sent to the peer. when not, simply reply
991 * the buffered packet to the peer.
993 * 0: the packet is received at the first time.
994 * 1: the packet was processed before.
995 * 2: the packet was processed before, but the address mismatches.
996 * -1: error happened.
999 check_recvdpkt(remote
, local
, rbuf
)
1000 struct sockaddr
*remote
, *local
;
1008 /* set current time */
1011 hash
= eay_md5_one(rbuf
);
1013 plog(LLV_ERROR
, LOCATION
, NULL
,
1014 "failed to allocate buffer.\n");
1018 LIST_FOREACH(r
, &rcptree
, chain
) {
1019 if (memcmp(hash
->v
, r
->hash
->v
, r
->hash
->l
) == 0)
1024 /* this is the first time to receive the packet */
1029 * the packet was processed before, but the remote address mismatches.
1030 * ignore the port to accomodate port changes (e.g. floating).
1032 if (cmpsaddrwop(remote
, r
->remote
) != 0) {
1037 * it should not check the local address because the packet
1038 * may arrive at other interface.
1041 /* check the previous time to send */
1042 if (t
- r
->time_send
< 1) {
1043 plog(LLV_WARNING
, LOCATION
, NULL
,
1044 "the packet retransmitted in a short time from %s\n",
1046 /*XXX should it be error ? */
1049 /* select the socket to be sent */
1050 s
= getsockmyaddr(r
->local
);
1054 /* resend the packet if needed */
1055 len
= sendfromto(s
, r
->sendbuf
->v
, r
->sendbuf
->l
,
1056 r
->local
, r
->remote
, lcconf
->count_persend
);
1058 plog(LLV_ERROR
, LOCATION
, NULL
, "sendfromto failed\n");
1062 /* check the retry counter */
1064 if (r
->retry_counter
<= 0) {
1067 plog(LLV_DEBUG
, LOCATION
, NULL
,
1068 "deleted the retransmission packet to %s.\n",
1077 * adding a hash of received packet into the received list.
1080 add_recvdpkt(remote
, local
, sbuf
, rbuf
, non_esp
)
1081 struct sockaddr
*remote
, *local
;
1082 vchar_t
*sbuf
, *rbuf
;
1085 struct recvdpkt
*new = NULL
;
1087 if (lcconf
->retry_counter
== 0) {
1088 /* no need to add it */
1092 new = racoon_calloc(1, sizeof(*new));
1094 plog(LLV_ERROR
, LOCATION
, NULL
,
1095 "failed to allocate buffer.\n");
1099 new->hash
= eay_md5_one(rbuf
);
1101 plog(LLV_ERROR
, LOCATION
, NULL
,
1102 "failed to allocate buffer.\n");
1106 new->remote
= dupsaddr(remote
);
1107 if (new->remote
== NULL
) {
1108 plog(LLV_ERROR
, LOCATION
, NULL
,
1109 "failed to allocate buffer.\n");
1113 new->local
= dupsaddr(local
);
1114 if (new->local
== NULL
) {
1115 plog(LLV_ERROR
, LOCATION
, NULL
,
1116 "failed to allocate buffer.\n");
1122 plog (LLV_DEBUG
, LOCATION
, NULL
, "Adding NON-ESP marker\n");
1124 /* If NAT-T port floating is in use, 4 zero bytes (non-ESP marker)
1125 must added just before the packet itself. For this we must
1126 allocate a new buffer and release it at the end. */
1127 if ((new->sendbuf
= vmalloc (sbuf
->l
+ non_esp
)) == NULL
) {
1128 plog(LLV_ERROR
, LOCATION
, NULL
,
1129 "failed to allocate extra buf for non-esp\n");
1133 *(u_int32_t
*)new->sendbuf
->v
= 0;
1134 memcpy(new->sendbuf
->v
+ non_esp
, sbuf
->v
, sbuf
->l
);
1136 new->sendbuf
= vdup(sbuf
);
1137 if (new->sendbuf
== NULL
) {
1138 plog(LLV_ERROR
, LOCATION
, NULL
,
1139 "failed to allocate buffer.\n");
1145 new->retry_counter
= lcconf
->retry_counter
;
1147 new->created
= time(NULL
);
1149 LIST_INSERT_HEAD(&rcptree
, new, chain
);
1159 racoon_free(r
->remote
);
1161 racoon_free(r
->local
);
1173 LIST_REMOVE(r
, chain
);
1177 sweep_recvdpkt(dummy
)
1180 struct recvdpkt
*r
, *next
;
1183 /* set current time */
1186 /* set the lifetime of the retransmission */
1187 lt
= lcconf
->retry_counter
* lcconf
->retry_interval
;
1189 for (r
= LIST_FIRST(&rcptree
); r
; r
= next
) {
1190 next
= LIST_NEXT(r
, chain
);
1192 if (t
- r
->created
> lt
) {
1198 sched_new(lt
, sweep_recvdpkt
, NULL
);
1204 struct recvdpkt
*r
, *next
;
1206 for (r
= LIST_FIRST(&rcptree
); r
; r
= next
) {
1207 next
= LIST_NEXT(r
, chain
);
1216 time_t lt
= lcconf
->retry_counter
* lcconf
->retry_interval
;
1218 LIST_INIT(&rcptree
);
1220 sched_new(lt
, sweep_recvdpkt
, NULL
);
1223 #ifdef ENABLE_HYBRID
1225 * Returns 0 if the address was obtained by ISAKMP mode config, 1 otherwise
1226 * This should be in isakmp_cfg.c but ph1tree being private, it must be there
1229 exclude_cfg_addr(addr
)
1230 const struct sockaddr
*addr
;
1232 struct ph1handle
*p
;
1233 struct sockaddr_in
*sin
;
1235 LIST_FOREACH(p
, &ph1tree
, chain
) {
1236 if ((p
->mode_cfg
!= NULL
) &&
1237 (p
->mode_cfg
->flags
& ISAKMP_CFG_GOT_ADDR4
) &&
1238 (addr
->sa_family
== AF_INET
)) {
1239 sin
= (struct sockaddr_in
*)addr
;
1240 if (sin
->sin_addr
.s_addr
== p
->mode_cfg
->addr4
.s_addr
)
1249 #ifdef ENABLE_HYBRID
1251 getph1bylogin(login
)
1254 struct ph1handle
*p
;
1256 LIST_FOREACH(p
, &ph1tree
, chain
) {
1257 if (p
->mode_cfg
== NULL
)
1259 if (strncmp(p
->mode_cfg
->login
, login
, LOGINLEN
) == 0)
1267 purgeph1bylogin(login
)
1270 struct ph1handle
*p
;
1273 LIST_FOREACH(p
, &ph1tree
, chain
) {
1274 if (p
->mode_cfg
== NULL
)
1276 if (strncmp(p
->mode_cfg
->login
, login
, LOGINLEN
) == 0) {
1277 if (p
->status
== PHASE1ST_ESTABLISHED
)
1278 isakmp_info_send_d1(p
);
1288 purgephXbydstaddrwop(remote
)
1289 struct sockaddr
*remote
;
1292 struct ph1handle
*p
;
1294 LIST_FOREACH(p
, &ph1tree
, chain
) {
1295 if (cmpsaddrwop(remote
, p
->remote
) == 0) {
1296 plog(LLV_WARNING
, LOCATION
, NULL
,
1297 "in %s... purging phase1 and related phase2s\n", __FUNCTION__
);
1298 ike_session_purge_ph2s_by_ph1(p
);
1299 if (p
->status
== PHASE1ST_ESTABLISHED
)
1300 isakmp_info_send_d1(p
);
1301 isakmp_ph1expire(p
);
1310 purgephXbyspid(u_int32_t spid
,
1313 struct ph2handle
*iph2
;
1314 struct ph1handle
*iph1
;
1316 // do ph2's first... we need the ph1s for notifications
1317 LIST_FOREACH(iph2
, &ph2tree
, chain
) {
1318 if (spid
== iph2
->spid
) {
1319 if (iph2
->status
== PHASE2ST_ESTABLISHED
) {
1320 isakmp_info_send_d2(iph2
);
1322 isakmp_ph2expire(iph2
); // iph2 will go down 1 second later.
1323 ike_session_stopped_by_controller(iph2
->parent_session
,
1324 ike_session_stopped_by_flush
);
1328 // do the ph1s last.
1329 LIST_FOREACH(iph2
, &ph2tree
, chain
) {
1330 if (spid
== iph2
->spid
) {
1331 if (del_boundph1
&& iph2
->parent_session
) {
1332 for (iph1
= LIST_FIRST(&iph2
->parent_session
->ikev1_state
.ph1tree
); iph1
; iph1
= LIST_NEXT(iph1
, ph1ofsession_chain
)) {
1333 if (iph1
->status
== PHASE1ST_ESTABLISHED
) {
1334 isakmp_info_send_d1(iph1
);
1336 isakmp_ph1expire(iph1
);
1347 ph1_force_dpd (struct sockaddr
*remote
)
1350 struct ph1handle
*p
;
1352 LIST_FOREACH(p
, &ph1tree
, chain
) {
1353 if (cmpsaddrwop(remote
, p
->remote
) == 0) {
1354 if (p
->status
== PHASE1ST_ESTABLISHED
&&
1357 p
->rmconf
->dpd_interval
) {
1359 isakmp_info_send_r_u(p
);
1362 plog(LLV_DEBUG2
, LOCATION
, NULL
, "skipping forced-DPD for phase1 (dpd already in progress).\n");
1365 plog(LLV_DEBUG2
, LOCATION
, NULL
, "skipping forced-DPD for phase1 (status %d, dying %d, dpd-support %d, dpd-interval %d).\n",
1366 p
->status
, p
->is_dying
, p
->dpd_support
, p
->rmconf
->dpd_interval
);