]>
git.saurik.com Git - apple/network_cmds.git/blob - racoon.tproj/session.c
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>
71 #include "grabmyaddr.h"
73 #include "isakmp_var.h"
74 #include "admin_var.h"
78 #include "localconf.h"
79 #include "remoteconf.h"
82 static void close_session
__P((void));
83 static void check_rtsock
__P((void *));
84 static void initfds
__P((void));
85 static void init_signal
__P((void));
86 static int set_signal
__P((int sig
, RETSIGTYPE (*func
) __P((int))));
87 static void check_sigreq
__P((void));
88 static void check_flushsa_stub
__P((void *));
89 static void check_flushsa
__P((void));
90 static int close_sockets
__P((void));
94 static int sigreq
= 0;
100 struct timeval
*timeout
;
104 /* initialize schedular */
109 #ifdef ENABLE_ADMINPORT
110 /* debug port has no authentication, do not open it */
111 if (admin_init() < 0)
117 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",
149 #ifdef ENABLE_ADMINPORT
150 if (FD_ISSET(lcconf
->sock_admin
, &rfds
))
154 for (p
= lcconf
->myaddrs
; p
; p
= p
->next
) {
157 if (FD_ISSET(p
->sock
, &rfds
))
158 isakmp_handler(p
->sock
);
160 if (p
->nattsock
>= 0 && FD_ISSET(p
->nattsock
, &rfds
))
161 isakmp_natt_handler(p
->nattsock
);
165 if (FD_ISSET(lcconf
->sock_pfkey
, &rfds
))
168 if (lcconf
->rtsock
>= 0 && FD_ISSET(lcconf
->rtsock
, &rfds
)) {
169 if (update_myaddrs() && lcconf
->autograbaddr
)
170 sched_new(5, check_rtsock
, NULL
);
176 /* clear all status and exit program. */
184 plog(LLV_INFO
, LOCATION
, NULL
, "racoon shutdown\n");
194 autoconf_myaddrsport();
197 /* initialize socket list again */
210 #ifdef ENABLE_ADMINPORT
211 if (lcconf
->sock_admin
>= FD_SETSIZE
) {
212 plog(LLV_ERROR
, LOCATION
, NULL
, "fd_set overrun\n");
215 FD_SET(lcconf
->sock_admin
, &mask0
);
216 nfds
= (nfds
> lcconf
->sock_admin
? nfds
: lcconf
->sock_admin
);
218 if (lcconf
->sock_pfkey
>= FD_SETSIZE
) {
219 plog(LLV_ERROR
, LOCATION
, NULL
, "fd_set overrun\n");
222 FD_SET(lcconf
->sock_pfkey
, &mask0
);
223 nfds
= (nfds
> lcconf
->sock_pfkey
? nfds
: lcconf
->sock_pfkey
);
224 if (lcconf
->rtsock
>= 0) {
225 if (lcconf
->rtsock
>= FD_SETSIZE
) {
226 plog(LLV_ERROR
, LOCATION
, NULL
, "fd_set overrun\n");
229 FD_SET(lcconf
->rtsock
, &mask0
);
230 nfds
= (nfds
> lcconf
->rtsock
? nfds
: lcconf
->rtsock
);
233 for (p
= lcconf
->myaddrs
; p
; p
= p
->next
) {
236 if (p
->sock
>= FD_SETSIZE
) {
237 plog(LLV_ERROR
, LOCATION
, NULL
, "fd_set overrun\n");
240 FD_SET(p
->sock
, &mask0
);
241 nfds
= (nfds
> p
->sock
? nfds
: p
->sock
);
243 if (p
->nattsock
>= 0) {
244 if (p
-> nattsock
>= FD_SETSIZE
) {
245 plog(LLV_ERROR
, LOCATION
, NULL
, "fd_set overrun\n");
248 FD_SET(p
->nattsock
, &mask0
);
249 nfds
= (nfds
> p
->nattsock
? nfds
: p
->nattsock
);
256 static int signals
[] = {
267 * asynchronous requests will actually dispatched in the
268 * main loop in session().
284 #ifdef DEBUG_RECORD_MALLOCATION
290 /* XXX should be blocked any signal ? */
296 extern int cfreparse(void);
307 plog(LLV_ERROR
, LOCATION
, NULL
,
308 "configuration read failed\n");
315 plog(LLV_INFO
, LOCATION
, NULL
, "caught signal %d\n", sigreq
);
316 pfkey_send_flush(lcconf
->sock_pfkey
, SADB_SATYPE_UNSPEC
);
317 sched_new(1, check_flushsa_stub
, NULL
);
324 * waiting the termination of processing until sending DELETE message
325 * for all inbound SA will complete.
328 check_flushsa_stub(p
)
339 struct sadb_msg
*msg
, *end
, *next
;
341 caddr_t mhp
[SADB_EXT_MAX
+ 1];
344 buf
= pfkey_dump_sadb(SADB_SATYPE_UNSPEC
);
346 msg
= (struct sadb_msg
*)buf
->v
;
347 end
= (struct sadb_msg
*)(buf
->v
+ buf
->l
);
349 /* counting SA except of dead one. */
352 if (PFKEY_UNUNIT64(msg
->sadb_msg_len
) < sizeof(*msg
))
354 next
= (struct sadb_msg
*)((caddr_t
)msg
+ PFKEY_UNUNIT64(msg
->sadb_msg_len
));
355 if (msg
->sadb_msg_type
!= SADB_DUMP
) {
360 if (pfkey_align(msg
, mhp
) || pfkey_check(mhp
)) {
361 plog(LLV_ERROR
, LOCATION
, NULL
,
362 "pfkey_check (%s)\n", ipsec_strerror());
367 sa
= (struct sadb_sa
*)(mhp
[SADB_EXT_SA
]);
373 if (sa
->sadb_sa_state
!= SADB_SASTATE_DEAD
) {
385 sched_new(1, check_flushsa_stub
, NULL
);
397 for (i
= 0; signals
[i
] != 0; i
++)
398 if (set_signal(signals
[i
], signal_handler
) < 0) {
399 plog(LLV_ERROR
, LOCATION
, NULL
,
400 "failed to set_signal (%s)\n",
407 set_signal(sig
, func
)
409 RETSIGTYPE (*func
) __P((int));
413 memset((caddr_t
)&sa
, 0, sizeof(sa
));
414 sa
.sa_handler
= func
;
415 sa
.sa_flags
= SA_RESTART
;
417 if (sigemptyset(&sa
.sa_mask
) < 0)
420 if (sigaction(sig
, &sa
, (struct sigaction
*)0) < 0)
430 pfkey_close(lcconf
->sock_pfkey
);
431 #ifdef ENABLE_ADMINPORT