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
);
735 if (iph2
->ext_nat_id
) {
736 vfree(iph2
->ext_nat_id
);
737 iph2
->ext_nat_id
= NULL
;
739 if (iph2
->ext_nat_id_p
) {
740 vfree(iph2
->ext_nat_id_p
);
741 iph2
->ext_nat_id_p
= NULL
;
749 * create new isakmp Phase 2 status record to handle isakmp in Phase2
753 struct ph2handle
*iph2
;
755 LIST_INSERT_HEAD(&ph2tree
, iph2
, chain
);
762 struct ph2handle
*iph2
;
764 LIST_REMOVE(iph2
, chain
);
774 flushph2(int ignore_established_handles
)
776 struct ph2handle
*p
, *next
;
778 plog(LLV_DEBUG2
, LOCATION
, NULL
,
779 "flushing all ph2 handlers...\n");
781 for (p
= LIST_FIRST(&ph2tree
); p
; p
= next
) {
782 next
= LIST_NEXT(p
, chain
);
783 if (p
->is_dying
|| p
->status
== PHASE2ST_EXPIRED
) {
786 if (p
->status
== PHASE2ST_ESTABLISHED
){
787 if (ignore_established_handles
) {
788 plog(LLV_DEBUG2
, LOCATION
, NULL
,
789 "skipping ph2 handler that's established...\n");
792 /* send delete information */
793 plog(LLV_DEBUG2
, LOCATION
, NULL
,
794 "got an established ph2 handler to flush...\n");
795 isakmp_info_send_d2(p
);
797 plog(LLV_DEBUG2
, LOCATION
, NULL
,
798 "got a ph2 handler to flush (state %d)\n", p
->status
);
801 ike_session_stopped_by_controller(p
->parent_session
,
802 ike_session_stopped_by_flush
);
811 * Delete all Phase 2 handlers for this src/dst/proto. This
812 * is used during INITIAL-CONTACT processing (so no need to
813 * send a message to the peer).
816 deleteallph2(src
, dst
, proto_id
)
817 struct sockaddr
*src
, *dst
;
820 struct ph2handle
*iph2
, *next
;
823 for (iph2
= LIST_FIRST(&ph2tree
); iph2
!= NULL
; iph2
= next
) {
824 next
= LIST_NEXT(iph2
, chain
);
825 if (iph2
->is_dying
|| iph2
->status
== PHASE2ST_EXPIRED
) {
828 if (iph2
->proposal
== NULL
&& iph2
->approval
== NULL
)
830 if (cmpsaddrwop(src
, iph2
->src
) != 0 ||
831 cmpsaddrwop(dst
, iph2
->dst
) != 0) {
834 if (iph2
->approval
!= NULL
) {
835 for (pr
= iph2
->approval
->head
; pr
!= NULL
;
837 if (proto_id
== pr
->proto_id
)
840 } else if (iph2
->proposal
!= NULL
) {
841 for (pr
= iph2
->proposal
->head
; pr
!= NULL
;
843 if (proto_id
== pr
->proto_id
)
849 plog(LLV_DEBUG2
, LOCATION
, NULL
,
850 "deleteallph2: got a ph2 handler...\n");
851 if (iph2
->status
== PHASE2ST_ESTABLISHED
)
852 isakmp_info_send_d2(iph2
);
853 ike_session_stopped_by_controller(iph2
->parent_session
,
854 ike_session_stopped_by_flush
);
862 * Delete all Phase 1 handlers for this src/dst.
865 deleteallph1(src
, dst
)
866 struct sockaddr
*src
, *dst
;
868 struct ph1handle
*iph1
, *next
;
870 for (iph1
= LIST_FIRST(&ph1tree
); iph1
!= NULL
; iph1
= next
) {
871 next
= LIST_NEXT(iph1
, chain
);
872 if (cmpsaddrwop(src
, iph1
->local
) != 0 ||
873 cmpsaddrwop(dst
, iph1
->remote
) != 0) {
876 plog(LLV_DEBUG2
, LOCATION
, NULL
,
877 "deleteallph1: got a ph1 handler...\n");
878 if (iph1
->status
== PHASE2ST_ESTABLISHED
)
879 isakmp_info_send_d1(iph1
);
881 ike_session_stopped_by_controller(iph1
->parent_session
,
882 ike_session_stopped_by_flush
);
891 struct ph1handle
*iph1
;
892 struct ph2handle
*iph2
;
894 if (iph2
->ph1
&& (struct ph1handle
*)iph2
->ph1bind
.le_next
== iph1
) {
895 plog(LLV_ERROR
, LOCATION
, NULL
, "duplicate %s.\n", __FUNCTION__
);
898 LIST_INSERT_HEAD(&iph1
->ph2tree
, iph2
, ph1bind
);
903 struct ph2handle
*iph2
;
905 if (iph2
->ph1
!= NULL
) {
906 plog(LLV_DEBUG
, LOCATION
, NULL
, "unbindph12.\n");
908 LIST_REMOVE(iph2
, ph1bind
);
913 rebindph12(new_ph1
, iph2
)
914 struct ph1handle
*new_ph1
;
915 struct ph2handle
*iph2
;
921 // reconcile the ph1-to-ph2 binding
922 plog(LLV_DEBUG
, LOCATION
, NULL
, "rebindph12.\n");
924 bindph12(new_ph1
, iph2
);
925 // recalculate ivm since ph1 binding has changed
926 if (iph2
->ivm
!= NULL
) {
927 oakley_delivm(iph2
->ivm
);
928 if (new_ph1
->status
== PHASE1ST_ESTABLISHED
) {
929 iph2
->ivm
= oakley_newiv2(new_ph1
, iph2
->msgid
);
930 plog(LLV_DEBUG
, LOCATION
, NULL
, "ph12 binding changed... recalculated ivm.\n");
937 /* %%% management contacted list */
939 * search contacted list.
943 struct sockaddr
*remote
;
947 LIST_FOREACH(p
, &ctdtree
, chain
) {
948 if (cmpsaddrstrict(remote
, p
->remote
) == 0)
956 * create new isakmp Phase 2 status record to handle isakmp in Phase2
960 struct sockaddr
*remote
;
962 struct contacted
*new;
964 /* create new iph2 */
965 new = racoon_calloc(1, sizeof(*new));
969 new->remote
= dupsaddr(remote
);
970 if (new->remote
== NULL
) {
971 plog(LLV_ERROR
, LOCATION
, NULL
,
972 "failed to allocate buffer.\n");
977 LIST_INSERT_HEAD(&ctdtree
, new, chain
);
986 struct contacted
*c
, *next
;
988 for (c
= LIST_FIRST(&ctdtree
); c
; c
= next
) {
989 next
= LIST_NEXT(c
, chain
);
990 LIST_REMOVE(c
, chain
);
991 racoon_free(c
->remote
);
1003 * check the response has been sent to the peer. when not, simply reply
1004 * the buffered packet to the peer.
1006 * 0: the packet is received at the first time.
1007 * 1: the packet was processed before.
1008 * 2: the packet was processed before, but the address mismatches.
1009 * -1: error happened.
1012 check_recvdpkt(remote
, local
, rbuf
)
1013 struct sockaddr
*remote
, *local
;
1021 /* set current time */
1024 hash
= eay_md5_one(rbuf
);
1026 plog(LLV_ERROR
, LOCATION
, NULL
,
1027 "failed to allocate buffer.\n");
1031 LIST_FOREACH(r
, &rcptree
, chain
) {
1032 if (memcmp(hash
->v
, r
->hash
->v
, r
->hash
->l
) == 0)
1037 /* this is the first time to receive the packet */
1042 * the packet was processed before, but the remote address mismatches.
1043 * ignore the port to accomodate port changes (e.g. floating).
1045 if (cmpsaddrwop(remote
, r
->remote
) != 0) {
1050 * it should not check the local address because the packet
1051 * may arrive at other interface.
1054 /* check the previous time to send */
1055 if (t
- r
->time_send
< 1) {
1056 plog(LLV_WARNING
, LOCATION
, NULL
,
1057 "the packet retransmitted in a short time from %s\n",
1059 /*XXX should it be error ? */
1062 /* select the socket to be sent */
1063 s
= getsockmyaddr(r
->local
);
1067 /* resend the packet if needed */
1068 len
= sendfromto(s
, r
->sendbuf
->v
, r
->sendbuf
->l
,
1069 r
->local
, r
->remote
, lcconf
->count_persend
);
1071 plog(LLV_ERROR
, LOCATION
, NULL
, "sendfromto failed\n");
1075 /* check the retry counter */
1077 if (r
->retry_counter
<= 0) {
1080 plog(LLV_DEBUG
, LOCATION
, NULL
,
1081 "deleted the retransmission packet to %s.\n",
1090 * adding a hash of received packet into the received list.
1093 add_recvdpkt(remote
, local
, sbuf
, rbuf
, non_esp
)
1094 struct sockaddr
*remote
, *local
;
1095 vchar_t
*sbuf
, *rbuf
;
1098 struct recvdpkt
*new = NULL
;
1100 if (lcconf
->retry_counter
== 0) {
1101 /* no need to add it */
1105 new = racoon_calloc(1, sizeof(*new));
1107 plog(LLV_ERROR
, LOCATION
, NULL
,
1108 "failed to allocate buffer.\n");
1112 new->hash
= eay_md5_one(rbuf
);
1114 plog(LLV_ERROR
, LOCATION
, NULL
,
1115 "failed to allocate buffer.\n");
1119 new->remote
= dupsaddr(remote
);
1120 if (new->remote
== NULL
) {
1121 plog(LLV_ERROR
, LOCATION
, NULL
,
1122 "failed to allocate buffer.\n");
1126 new->local
= dupsaddr(local
);
1127 if (new->local
== NULL
) {
1128 plog(LLV_ERROR
, LOCATION
, NULL
,
1129 "failed to allocate buffer.\n");
1135 plog (LLV_DEBUG
, LOCATION
, NULL
, "Adding NON-ESP marker\n");
1137 /* If NAT-T port floating is in use, 4 zero bytes (non-ESP marker)
1138 must added just before the packet itself. For this we must
1139 allocate a new buffer and release it at the end. */
1140 if ((new->sendbuf
= vmalloc (sbuf
->l
+ non_esp
)) == NULL
) {
1141 plog(LLV_ERROR
, LOCATION
, NULL
,
1142 "failed to allocate extra buf for non-esp\n");
1146 *(u_int32_t
*)new->sendbuf
->v
= 0;
1147 memcpy(new->sendbuf
->v
+ non_esp
, sbuf
->v
, sbuf
->l
);
1149 new->sendbuf
= vdup(sbuf
);
1150 if (new->sendbuf
== NULL
) {
1151 plog(LLV_ERROR
, LOCATION
, NULL
,
1152 "failed to allocate buffer.\n");
1158 new->retry_counter
= lcconf
->retry_counter
;
1160 new->created
= time(NULL
);
1162 LIST_INSERT_HEAD(&rcptree
, new, chain
);
1172 racoon_free(r
->remote
);
1174 racoon_free(r
->local
);
1186 LIST_REMOVE(r
, chain
);
1190 sweep_recvdpkt(dummy
)
1193 struct recvdpkt
*r
, *next
;
1196 /* set current time */
1199 /* set the lifetime of the retransmission */
1200 lt
= lcconf
->retry_counter
* lcconf
->retry_interval
;
1202 for (r
= LIST_FIRST(&rcptree
); r
; r
= next
) {
1203 next
= LIST_NEXT(r
, chain
);
1205 if (t
- r
->created
> lt
) {
1211 sched_new(lt
, sweep_recvdpkt
, &rcptree
);
1217 struct recvdpkt
*r
, *next
;
1219 for (r
= LIST_FIRST(&rcptree
); r
; r
= next
) {
1220 next
= LIST_NEXT(r
, chain
);
1224 sched_scrub_param(&rcptree
);
1230 time_t lt
= lcconf
->retry_counter
* lcconf
->retry_interval
;
1232 LIST_INIT(&rcptree
);
1234 sched_new(lt
, sweep_recvdpkt
, &rcptree
);
1237 #ifdef ENABLE_HYBRID
1239 * Returns 0 if the address was obtained by ISAKMP mode config, 1 otherwise
1240 * This should be in isakmp_cfg.c but ph1tree being private, it must be there
1243 exclude_cfg_addr(addr
)
1244 const struct sockaddr
*addr
;
1246 struct ph1handle
*p
;
1247 struct sockaddr_in
*sin
;
1249 LIST_FOREACH(p
, &ph1tree
, chain
) {
1250 if ((p
->mode_cfg
!= NULL
) &&
1251 (p
->mode_cfg
->flags
& ISAKMP_CFG_GOT_ADDR4
) &&
1252 (addr
->sa_family
== AF_INET
)) {
1253 sin
= (struct sockaddr_in
*)addr
;
1254 if (sin
->sin_addr
.s_addr
== p
->mode_cfg
->addr4
.s_addr
)
1263 #ifdef ENABLE_HYBRID
1265 getph1bylogin(login
)
1268 struct ph1handle
*p
;
1270 LIST_FOREACH(p
, &ph1tree
, chain
) {
1271 if (p
->mode_cfg
== NULL
)
1273 if (strncmp(p
->mode_cfg
->login
, login
, LOGINLEN
) == 0)
1281 purgeph1bylogin(login
)
1284 struct ph1handle
*p
;
1287 LIST_FOREACH(p
, &ph1tree
, chain
) {
1288 if (p
->mode_cfg
== NULL
)
1290 if (strncmp(p
->mode_cfg
->login
, login
, LOGINLEN
) == 0) {
1291 if (p
->status
== PHASE1ST_ESTABLISHED
)
1292 isakmp_info_send_d1(p
);
1302 purgephXbydstaddrwop(remote
)
1303 struct sockaddr
*remote
;
1306 struct ph1handle
*p
;
1308 LIST_FOREACH(p
, &ph1tree
, chain
) {
1309 if (cmpsaddrwop(remote
, p
->remote
) == 0) {
1310 plog(LLV_WARNING
, LOCATION
, NULL
,
1311 "in %s... purging phase1 and related phase2s\n", __FUNCTION__
);
1312 ike_session_purge_ph2s_by_ph1(p
);
1313 if (p
->status
== PHASE1ST_ESTABLISHED
)
1314 isakmp_info_send_d1(p
);
1315 isakmp_ph1expire(p
);
1324 purgephXbyspid(u_int32_t spid
,
1327 struct ph2handle
*iph2
;
1328 struct ph1handle
*iph1
;
1330 // do ph2's first... we need the ph1s for notifications
1331 LIST_FOREACH(iph2
, &ph2tree
, chain
) {
1332 if (spid
== iph2
->spid
) {
1333 if (iph2
->is_dying
|| iph2
->status
== PHASE2ST_EXPIRED
) {
1336 if (iph2
->status
== PHASE2ST_ESTABLISHED
) {
1337 isakmp_info_send_d2(iph2
);
1339 ike_session_stopped_by_controller(iph2
->parent_session
,
1340 ike_session_stopped_by_flush
);
1341 isakmp_ph2expire(iph2
); // iph2 will go down 1 second later.
1345 // do the ph1s last.
1346 LIST_FOREACH(iph2
, &ph2tree
, chain
) {
1347 if (spid
== iph2
->spid
) {
1348 if (del_boundph1
&& iph2
->parent_session
) {
1349 for (iph1
= LIST_FIRST(&iph2
->parent_session
->ikev1_state
.ph1tree
); iph1
; iph1
= LIST_NEXT(iph1
, ph1ofsession_chain
)) {
1350 if (iph1
->is_dying
|| iph1
->status
== PHASE1ST_EXPIRED
) {
1353 if (iph1
->status
== PHASE1ST_ESTABLISHED
) {
1354 isakmp_info_send_d1(iph1
);
1356 isakmp_ph1expire(iph1
);
1367 ph1_force_dpd (struct sockaddr
*remote
)
1370 struct ph1handle
*p
;
1372 LIST_FOREACH(p
, &ph1tree
, chain
) {
1373 if (cmpsaddrwop(remote
, p
->remote
) == 0) {
1374 if (p
->status
== PHASE1ST_ESTABLISHED
&&
1377 p
->rmconf
->dpd_interval
) {
1379 isakmp_info_send_r_u(p
);
1382 plog(LLV_DEBUG2
, LOCATION
, NULL
, "skipping forced-DPD for phase1 (dpd already in progress).\n");
1385 plog(LLV_DEBUG2
, LOCATION
, NULL
, "skipping forced-DPD for phase1 (status %d, dying %d, dpd-support %d, dpd-interval %d).\n",
1386 p
->status
, p
->is_dying
, p
->dpd_support
, p
->rmconf
->dpd_interval
);