]>
git.saurik.com Git - apple/network_cmds.git/blob - racoon.tproj/handler.c
1ca5b5cb655ffa63f27c134ced99552e6dd7a6a1
1 /* $KAME: handler.c,v 1.56 2002/01/02 09:05:25 jinmei Exp $ */
4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the project nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/types.h>
33 #include <sys/param.h>
34 #include <sys/socket.h>
50 #include "grabmyaddr.h"
51 #include "algorithm.h"
52 #include "crypto_openssl.h"
55 #include "isakmp_var.h"
57 #include "isakmp_inf.h"
59 #include "remoteconf.h"
60 #include "localconf.h"
68 static LIST_HEAD(_ph1tree_
, ph1handle
) ph1tree
;
69 static LIST_HEAD(_ph2tree_
, ph2handle
) ph2tree
;
70 static LIST_HEAD(_ctdtree_
, contacted
) ctdtree
;
71 static LIST_HEAD(_rcptree_
, recvdpkt
) rcptree
;
73 static void del_recvdpkt
__P((struct recvdpkt
*));
74 static void rem_recvdpkt
__P((struct recvdpkt
*));
75 static void sweep_recvdpkt
__P((void *));
78 * functions about management of the isakmp status table
80 /* %%% management phase 1 handler */
82 * search for isakmpsa handler with isakmp index.
85 extern caddr_t
val2str(const char *, size_t);
93 LIST_FOREACH(p
, &ph1tree
, chain
) {
94 if (p
->status
== PHASE1ST_EXPIRED
)
96 if (memcmp(&p
->index
, index
, sizeof(*index
)) == 0)
104 * search for isakmp handler by i_ck in index.
107 getph1byindex0(index
)
112 LIST_FOREACH(p
, &ph1tree
, chain
) {
113 if (p
->status
== PHASE1ST_EXPIRED
)
115 if (memcmp(&p
->index
, index
, sizeof(cookie_t
)) == 0)
123 * search for isakmpsa handler by remote address.
124 * don't use port number to search because this function search
125 * with phase 2's destinaion.
128 getph1byaddr(local
, remote
)
129 struct sockaddr
*local
, *remote
;
133 LIST_FOREACH(p
, &ph1tree
, chain
) {
134 if (p
->status
== PHASE1ST_EXPIRED
)
136 if (cmpsaddrwop(local
, p
->local
) == 0
137 && cmpsaddrwop(remote
, p
->remote
) == 0)
150 struct ph1handle
*iph1
;
155 /* get length of buffer */
156 LIST_FOREACH(iph1
, &ph1tree
, chain
)
159 buf
= vmalloc(cnt
* sizeof(struct ph1dump
));
161 plog(LLV_ERROR
, LOCATION
, NULL
,
162 "failed to get buffer\n");
165 pd
= (struct ph1dump
*)buf
->v
;
167 LIST_FOREACH(iph1
, &ph1tree
, chain
) {
168 memcpy(&pd
->index
, &iph1
->index
, sizeof(iph1
->index
));
169 pd
->status
= iph1
->status
;
170 pd
->side
= iph1
->side
;
171 memcpy(&pd
->remote
, iph1
->remote
, iph1
->remote
->sa_len
);
172 memcpy(&pd
->local
, iph1
->local
, iph1
->local
->sa_len
);
173 pd
->version
= iph1
->version
;
174 pd
->etype
= iph1
->etype
;
175 pd
->created
= iph1
->created
;
176 pd
->ph2cnt
= iph1
->ph2cnt
;
184 * create new isakmp Phase 1 status record to handle isakmp in Phase1
189 struct ph1handle
*iph1
;
191 /* create new iph1 */
192 iph1
= racoon_calloc(1, sizeof(*iph1
));
196 iph1
->status
= PHASE1ST_SPAWN
;
202 * delete new isakmp Phase 1 status record to handle isakmp in Phase1
206 struct ph1handle
*iph1
;
209 racoon_free(iph1
->remote
);
213 racoon_free(iph1
->local
);
217 VPTRINIT(iph1
->authstr
);
219 sched_scrub_param(iph1
);
223 VPTRINIT(iph1
->sendbuf
);
225 VPTRINIT(iph1
->dhpriv
);
226 VPTRINIT(iph1
->dhpub
);
227 VPTRINIT(iph1
->dhpub_p
);
228 VPTRINIT(iph1
->dhgxy
);
229 VPTRINIT(iph1
->nonce
);
230 VPTRINIT(iph1
->nonce_p
);
231 VPTRINIT(iph1
->skeyid
);
232 VPTRINIT(iph1
->skeyid_d
);
233 VPTRINIT(iph1
->skeyid_a
);
234 VPTRINIT(iph1
->skeyid_e
);
236 VPTRINIT(iph1
->hash
);
238 VPTRINIT(iph1
->sig_p
);
239 oakley_delcert(iph1
->cert
);
241 oakley_delcert(iph1
->cert_p
);
243 oakley_delcert(iph1
->crl_p
);
245 oakley_delcert(iph1
->cr_p
);
248 VPTRINIT(iph1
->id_p
);
251 oakley_delivm(iph1
->ivm
);
256 VPTRINIT(iph1
->sa_ret
);
259 VPTRINIT(iph1
->gi_i
);
260 VPTRINIT(iph1
->gi_r
);
262 gssapi_free_state(iph1
);
269 * create new isakmp Phase 1 status record to handle isakmp in Phase1
273 struct ph1handle
*iph1
;
276 if (iph1
->remote
== NULL
) {
277 plog(LLV_ERROR
, LOCATION
, NULL
,
278 "invalid isakmp SA handler. no remote address.\n");
281 LIST_INSERT_HEAD(&ph1tree
, iph1
, chain
);
288 struct ph1handle
*iph1
;
290 LIST_REMOVE(iph1
, chain
);
299 struct ph1handle
*p
, *next
;
301 for (p
= LIST_FIRST(&ph1tree
); p
; p
= next
) {
302 next
= LIST_NEXT(p
, chain
);
304 /* send delete information */
305 if (p
->status
== PHASE1ST_ESTABLISHED
)
306 isakmp_info_send_d1(p
);
319 /* %%% management phase 2 handler */
321 * search ph2handle with policy id.
329 LIST_FOREACH(p
, &ph2tree
, chain
) {
331 * there are ph2handle independent on policy
332 * such like informational exchange.
342 * search ph2handle with sequence number.
350 LIST_FOREACH(p
, &ph2tree
, chain
) {
359 * search ph2handle with message id.
362 getph2bymsgid(iph1
, msgid
)
363 struct ph1handle
*iph1
;
368 LIST_FOREACH(p
, &ph2tree
, chain
) {
369 if (p
->msgid
== msgid
)
377 * call by pk_recvexpire().
380 getph2bysaidx(src
, dst
, proto_id
, spi
)
381 struct sockaddr
*src
, *dst
;
385 struct ph2handle
*iph2
;
388 LIST_FOREACH(iph2
, &ph2tree
, chain
) {
389 if (iph2
->proposal
== NULL
&& iph2
->approval
== NULL
)
391 if (iph2
->approval
!= NULL
) {
392 for (pr
= iph2
->approval
->head
; pr
!= NULL
;
394 if (proto_id
!= pr
->proto_id
)
396 if (spi
== pr
->spi
|| spi
== pr
->spi_p
)
399 } else if (iph2
->proposal
!= NULL
) {
400 for (pr
= iph2
->proposal
->head
; pr
!= NULL
;
402 if (proto_id
!= pr
->proto_id
)
414 * create new isakmp Phase 2 status record to handle isakmp in Phase2
419 struct ph2handle
*iph2
= NULL
;
421 /* create new iph2 */
422 iph2
= racoon_calloc(1, sizeof(*iph2
));
426 iph2
->status
= PHASE1ST_SPAWN
;
432 * initialize ph2handle
433 * NOTE: don't initialize src/dst.
434 * SPI in the proposal is cleared.
438 struct ph2handle
*iph2
;
440 sched_scrub_param(iph2
);
444 VPTRINIT(iph2
->sendbuf
);
445 VPTRINIT(iph2
->msg1
);
447 /* clear spi, keep variables in the proposal */
448 if (iph2
->proposal
) {
450 for (pr
= iph2
->proposal
->head
; pr
!= NULL
; pr
= pr
->next
)
455 if (iph2
->approval
) {
456 flushsaprop(iph2
->approval
);
457 iph2
->approval
= NULL
;
460 /* clear the generated policy */
461 if (iph2
->spidx_gen
) {
462 delsp_bothdir((struct policyindex
*)iph2
->spidx_gen
);
463 racoon_free(iph2
->spidx_gen
);
464 iph2
->spidx_gen
= NULL
;
468 oakley_dhgrp_free(iph2
->pfsgrp
);
472 VPTRINIT(iph2
->dhpriv
);
473 VPTRINIT(iph2
->dhpub
);
474 VPTRINIT(iph2
->dhpub_p
);
475 VPTRINIT(iph2
->dhgxy
);
477 VPTRINIT(iph2
->id_p
);
478 VPTRINIT(iph2
->nonce
);
479 VPTRINIT(iph2
->nonce_p
);
481 VPTRINIT(iph2
->sa_ret
);
484 oakley_delivm(iph2
->ivm
);
490 * delete new isakmp Phase 2 status record to handle isakmp in Phase2
494 struct ph2handle
*iph2
;
499 racoon_free(iph2
->src
);
503 racoon_free(iph2
->dst
);
507 racoon_free(iph2
->src_id
);
511 racoon_free(iph2
->dst_id
);
515 if (iph2
->proposal
) {
516 flushsaprop(iph2
->proposal
);
517 iph2
->proposal
= NULL
;
524 * create new isakmp Phase 2 status record to handle isakmp in Phase2
528 struct ph2handle
*iph2
;
530 LIST_INSERT_HEAD(&ph2tree
, iph2
, chain
);
537 struct ph2handle
*iph2
;
539 LIST_REMOVE(iph2
, chain
);
551 struct ph2handle
*p
, *next
;
553 for (p
= LIST_FIRST(&ph2tree
); p
; p
= next
) {
554 next
= LIST_NEXT(p
, chain
);
556 /* send delete information */
557 if (p
->status
== PHASE2ST_ESTABLISHED
)
558 isakmp_info_send_d2(p
);
567 * Delete all Phase 2 handlers for this src/dst/proto. This
568 * is used during INITIAL-CONTACT processing (so no need to
569 * send a message to the peer).
572 deleteallph2(src
, dst
, proto_id
)
573 struct sockaddr
*src
, *dst
;
576 struct ph2handle
*iph2
, *next
;
579 for (iph2
= LIST_FIRST(&ph2tree
); iph2
!= NULL
; iph2
= next
) {
580 next
= LIST_NEXT(iph2
, chain
);
581 if (iph2
->proposal
== NULL
&& iph2
->approval
== NULL
)
583 if (iph2
->approval
!= NULL
) {
584 for (pr
= iph2
->approval
->head
; pr
!= NULL
;
586 if (proto_id
== pr
->proto_id
)
589 } else if (iph2
->proposal
!= NULL
) {
590 for (pr
= iph2
->proposal
->head
; pr
!= NULL
;
592 if (proto_id
== pr
->proto_id
)
607 struct ph1handle
*iph1
;
608 struct ph2handle
*iph2
;
611 LIST_INSERT_HEAD(&iph1
->ph2tree
, iph2
, ph1bind
);
616 struct ph2handle
*iph2
;
618 if (iph2
->ph1
!= NULL
) {
620 LIST_REMOVE(iph2
, ph1bind
);
624 /* %%% management contacted list */
626 * search contacted list.
630 struct sockaddr
*remote
;
634 LIST_FOREACH(p
, &ctdtree
, chain
) {
635 if (cmpsaddrstrict(remote
, p
->remote
) == 0)
643 * create new isakmp Phase 2 status record to handle isakmp in Phase2
647 struct sockaddr
*remote
;
649 struct contacted
*new;
651 /* create new iph2 */
652 new = racoon_calloc(1, sizeof(*new));
656 new->remote
= dupsaddr(remote
);
658 LIST_INSERT_HEAD(&ctdtree
, new, chain
);
670 * check the response has been sent to the peer. when not, simply reply
671 * the buffered packet to the peer.
673 * 0: the packet is received at the first time.
674 * 1: the packet was processed before.
675 * 2: the packet was processed before, but the address mismatches.
676 * -1: error happened.
679 check_recvdpkt(remote
, local
, rbuf
)
680 struct sockaddr
*remote
, *local
;
688 /* set current time */
691 hash
= eay_md5_one(rbuf
);
693 plog(LLV_ERROR
, LOCATION
, NULL
,
694 "failed to allocate buffer.\n");
698 LIST_FOREACH(r
, &rcptree
, chain
) {
699 if (memcmp(hash
->v
, r
->hash
->v
, r
->hash
->l
) == 0)
704 /* this is the first time to receive the packet */
709 * the packet was processed before, but the remote address mismatches.
711 if (cmpsaddrstrict(remote
, r
->remote
) != 0)
715 * it should not check the local address because the packet
716 * may arrive at other interface.
719 /* check the previous time to send */
720 if (t
- r
->time_send
< 1) {
721 plog(LLV_WARNING
, LOCATION
, NULL
,
722 "the packet retransmitted in a short time from %s\n",
724 /*XXX should it be error ? */
727 /* select the socket to be sent */
728 s
= getsockmyaddr(r
->local
);
732 /* resend the packet if needed */
733 len
= sendfromto(s
, r
->sendbuf
->v
, r
->sendbuf
->l
,
734 r
->local
, r
->remote
, lcconf
->count_persend
);
736 plog(LLV_ERROR
, LOCATION
, NULL
, "sendfromto failed\n");
740 /* check the retry counter */
742 if (r
->retry_counter
<= 0) {
745 plog(LLV_DEBUG
, LOCATION
, NULL
,
746 "deleted the retransmission packet to %s.\n",
755 * adding a hash of received packet into the received list.
758 add_recvdpkt(remote
, local
, sbuf
, rbuf
)
759 struct sockaddr
*remote
, *local
;
760 vchar_t
*sbuf
, *rbuf
;
762 struct recvdpkt
*new = NULL
;
764 if (lcconf
->retry_counter
== 0) {
765 /* no need to add it */
769 new = racoon_calloc(1, sizeof(*new));
771 plog(LLV_ERROR
, LOCATION
, NULL
,
772 "failed to allocate buffer.\n");
776 new->hash
= eay_md5_one(rbuf
);
778 plog(LLV_ERROR
, LOCATION
, NULL
,
779 "failed to allocate buffer.\n");
783 new->remote
= dupsaddr(remote
);
784 if (new->remote
== NULL
) {
785 plog(LLV_ERROR
, LOCATION
, NULL
,
786 "failed to allocate buffer.\n");
790 new->local
= dupsaddr(local
);
791 if (new->local
== NULL
) {
792 plog(LLV_ERROR
, LOCATION
, NULL
,
793 "failed to allocate buffer.\n");
797 new->sendbuf
= vdup(sbuf
);
798 if (new->sendbuf
== NULL
) {
799 plog(LLV_ERROR
, LOCATION
, NULL
,
800 "failed to allocate buffer.\n");
805 new->retry_counter
= lcconf
->retry_counter
;
807 new->created
= time(NULL
);
809 LIST_INSERT_HEAD(&rcptree
, new, chain
);
819 racoon_free(r
->remote
);
821 racoon_free(r
->local
);
833 LIST_REMOVE(r
, chain
);
837 sweep_recvdpkt(dummy
)
840 struct recvdpkt
*r
, *next
;
843 /* set current time */
846 /* set the lifetime of the retransmission */
847 lt
= lcconf
->retry_counter
* lcconf
->retry_interval
;
849 for (r
= LIST_FIRST(&rcptree
); r
; r
= next
) {
850 next
= LIST_NEXT(r
, chain
);
852 if (t
- r
->created
> lt
) {
858 sched_new(lt
, sweep_recvdpkt
, NULL
);
864 time_t lt
= lcconf
->retry_counter
* lcconf
->retry_interval
;
868 sched_new(lt
, sweep_recvdpkt
, NULL
);