]>
git.saurik.com Git - apple/network_cmds.git/blob - racoon.tproj/session.c
bdd29755dadd6958261f1f1c8e5217269baca12d
1 /* $KAME: session.c,v 1.31 2002/11/20 02:06:18 itojun 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>
35 #include <sys/socket.h>
37 # include <sys/wait.h>
40 # define WEXITSTATUS(s) ((unsigned)(s) >> 8)
43 # define WIFEXITED(s) (((s) & 255) == 0)
46 #ifdef IPV6_INRIA_VERSION
47 #include <netinet/ipsec.h>
49 #include <netinet6/ipsec.h>
72 #include "grabmyaddr.h"
74 #include "isakmp_var.h"
75 #include "admin_var.h"
79 #include "localconf.h"
80 #include "remoteconf.h"
83 static void close_session
__P((void));
84 static void check_rtsock
__P((void *));
85 static void initfds
__P((void));
86 static void init_signal
__P((void));
87 static int set_signal
__P((int sig
, RETSIGTYPE (*func
) __P((int))));
88 static void check_sigreq
__P((void));
89 static void check_flushsa_stub
__P((void *));
90 static void check_flushsa
__P((void));
91 static int close_sockets
__P((void));
95 static int sigreq
= 0;
101 struct timeval
*timeout
;
105 /* initialize schedular */
110 #ifdef ENABLE_ADMINPORT
111 /* debug port has no authentication, do not open it */
112 if (admin_init() < 0)
118 if (isakmp_init() < 0)
127 * asynchronous requests via signal.
128 * make sure to reset sigreq to 0.
133 timeout
= schedular();
135 error
= select(nfds
, &rfds
, (fd_set
*)0, (fd_set
*)0, timeout
);
141 plog(LLV_ERROR
, LOCATION
, NULL
,
142 "failed to select (%s)\n",
148 #ifdef ENABLE_ADMINPORT
149 if (FD_ISSET(lcconf
->sock_admin
, &rfds
))
153 for (p
= lcconf
->myaddrs
; p
; p
= p
->next
) {
157 if (FD_ISSET(p
->sock
, &rfds
))
158 if ((error
= isakmp_handler(p
->sock
)) == -2)
163 if (p
->nattsock
>= 0 && FD_ISSET(p
->nattsock
, &rfds
))
164 if ((error
= isakmp_natt_handler(p
->nattsock
)) == -2)
169 if (lcconf
->autograbaddr
) {
170 /* serious socket problem - close all listening sockets and re-open */
173 sched_new(5, check_rtsock
, NULL
);
176 isakmp_close_sockets();
182 if (FD_ISSET(lcconf
->sock_pfkey
, &rfds
))
185 if (lcconf
->rtsock
>= 0 && FD_ISSET(lcconf
->rtsock
, &rfds
))
186 if (update_myaddrs() && lcconf
->autograbaddr
)
187 sched_new(5, check_rtsock
, NULL
);
191 /* clear all status and exit program. */
199 plog(LLV_INFO
, LOCATION
, NULL
, "racoon shutdown\n");
209 isakmp_close_unused();
210 autoconf_myaddrsport();
213 /* initialize socket list again */
226 #ifdef ENABLE_ADMINPORT
227 if (lcconf
->sock_admin
>= FD_SETSIZE
) {
228 plog(LLV_ERROR
, LOCATION
, NULL
, "fd_set overrun\n");
231 FD_SET(lcconf
->sock_admin
, &mask0
);
232 nfds
= (nfds
> lcconf
->sock_admin
? nfds
: lcconf
->sock_admin
);
234 if (lcconf
->sock_pfkey
>= FD_SETSIZE
) {
235 plog(LLV_ERROR
, LOCATION
, NULL
, "fd_set overrun\n");
238 FD_SET(lcconf
->sock_pfkey
, &mask0
);
239 nfds
= (nfds
> lcconf
->sock_pfkey
? nfds
: lcconf
->sock_pfkey
);
240 if (lcconf
->rtsock
>= 0) {
241 if (lcconf
->rtsock
>= FD_SETSIZE
) {
242 plog(LLV_ERROR
, LOCATION
, NULL
, "fd_set overrun\n");
245 FD_SET(lcconf
->rtsock
, &mask0
);
246 nfds
= (nfds
> lcconf
->rtsock
? nfds
: lcconf
->rtsock
);
249 for (p
= lcconf
->myaddrs
; p
; p
= p
->next
) {
252 if (p
->sock
>= FD_SETSIZE
) {
253 plog(LLV_ERROR
, LOCATION
, NULL
, "fd_set overrun\n");
256 FD_SET(p
->sock
, &mask0
);
257 nfds
= (nfds
> p
->sock
? nfds
: p
->sock
);
259 if (p
->nattsock
>= 0) {
260 if (p
-> nattsock
>= FD_SETSIZE
) {
261 plog(LLV_ERROR
, LOCATION
, NULL
, "fd_set overrun\n");
264 FD_SET(p
->nattsock
, &mask0
);
265 nfds
= (nfds
> p
->nattsock
? nfds
: p
->nattsock
);
272 static int signals
[] = {
283 * asynchronous requests will actually dispatched in the
284 * main loop in session().
300 #ifdef DEBUG_RECORD_MALLOCATION
306 /* XXX should be blocked any signal ? */
312 extern int cfreparse(void);
323 plog(LLV_ERROR
, LOCATION
, NULL
,
324 "configuration read failed\n");
331 plog(LLV_INFO
, LOCATION
, NULL
, "caught signal %d\n", sigreq
);
332 pfkey_send_flush(lcconf
->sock_pfkey
, SADB_SATYPE_UNSPEC
);
333 sched_new(1, check_flushsa_stub
, NULL
);
340 * waiting the termination of processing until sending DELETE message
341 * for all inbound SA will complete.
344 check_flushsa_stub(p
)
355 struct sadb_msg
*msg
, *end
, *next
;
357 caddr_t mhp
[SADB_EXT_MAX
+ 1];
360 buf
= pfkey_dump_sadb(SADB_SATYPE_UNSPEC
);
362 msg
= (struct sadb_msg
*)buf
->v
;
363 end
= (struct sadb_msg
*)(buf
->v
+ buf
->l
);
365 /* counting SA except of dead one. */
368 if (PFKEY_UNUNIT64(msg
->sadb_msg_len
) < sizeof(*msg
))
370 next
= (struct sadb_msg
*)((caddr_t
)msg
+ PFKEY_UNUNIT64(msg
->sadb_msg_len
));
371 if (msg
->sadb_msg_type
!= SADB_DUMP
) {
376 if (pfkey_align(msg
, mhp
) || pfkey_check(mhp
)) {
377 plog(LLV_ERROR
, LOCATION
, NULL
,
378 "pfkey_check (%s)\n", ipsec_strerror());
383 sa
= (struct sadb_sa
*)(mhp
[SADB_EXT_SA
]);
389 if (sa
->sadb_sa_state
!= SADB_SASTATE_DEAD
) {
401 sched_new(1, check_flushsa_stub
, NULL
);
413 for (i
= 0; signals
[i
] != 0; i
++)
414 if (set_signal(signals
[i
], signal_handler
) < 0) {
415 plog(LLV_ERROR
, LOCATION
, NULL
,
416 "failed to set_signal (%s)\n",
423 set_signal(sig
, func
)
425 RETSIGTYPE (*func
) __P((int));
429 memset((caddr_t
)&sa
, 0, sizeof(sa
));
430 sa
.sa_handler
= func
;
431 sa
.sa_flags
= SA_RESTART
;
433 if (sigemptyset(&sa
.sa_mask
) < 0)
436 if (sigaction(sig
, &sa
, (struct sigaction
*)0) < 0)
446 pfkey_close(lcconf
->sock_pfkey
);
447 #ifdef ENABLE_ADMINPORT