1 /* $Id: vpn_control.c,v 1.17.2.4 2005/07/12 11:49:44 manubsd Exp $ */
4 * Copyright (c) 2006 Apple Computer, Inc. All rights reserved.
6 * @APPLE_LICENSE_HEADER_START@
8 * The contents of this file constitute Original Code as defined in and
9 * are subject to the Apple Public Source License Version 1.1 (the
10 * "License"). You may not use this file except in compliance with the
11 * License. Please obtain a copy of the License at
12 * http://www.apple.com/publicsource and read it before using this file.
14 * This Original Code and all software distributed under the License are
15 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
19 * License for the specific language governing rights and limitations
22 * @APPLE_LICENSE_HEADER_END@
26 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
27 * All rights reserved.
29 * Redistribution and use in source and binary forms, with or without
30 * modification, are permitted provided that the following conditions
32 * 1. Redistributions of source code must retain the above copyright
33 * notice, this list of conditions and the following disclaimer.
34 * 2. Redistributions in binary form must reproduce the above copyright
35 * notice, this list of conditions and the following disclaimer in the
36 * documentation and/or other materials provided with the distribution.
37 * 3. Neither the name of the project nor the names of its contributors
38 * may be used to endorse or promote products derived from this software
39 * without specific prior written permission.
41 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
56 #include <sys/types.h>
57 #include <sys/param.h>
58 #include <sys/socket.h>
59 #include <sys/signal.h>
63 #include <System/net/pfkeyv2.h>
65 #include <netinet/in.h>
66 #ifndef HAVE_NETINET6_IPSEC
67 #include <netinet/ipsec.h>
69 #include <netinet6/ipsec.h>
91 #include "localconf.h"
92 #include "remoteconf.h"
93 #include "grabmyaddr.h"
94 #include "isakmp_var.h"
100 #include "ipsec_doi.h"
101 #include "vpn_control.h"
102 #include "vpn_control_var.h"
103 #include "isakmp_inf.h"
105 #include "gcmalloc.h"
107 #ifdef ENABLE_VPNCONTROL_PORT
108 char *vpncontrolsock_path
= VPNCONTROLSOCK_PATH
;
109 uid_t vpncontrolsock_owner
= 0;
110 gid_t vpncontrolsock_group
= 0;
111 mode_t vpncontrolsock_mode
= 0600;
113 static struct sockaddr_un sunaddr
;
114 static int vpncontrol_process(struct vpnctl_socket_elem
*, char *);
115 static int vpncontrol_reply(int, char *);
116 static void vpncontrol_close_comm(struct vpnctl_socket_elem
*);
117 static int checklaunchd();
118 extern int vpn_get_config
__P((struct ph1handle
*, struct vpnctl_status_phase_change
**, size_t *));
119 extern int vpn_xauth_reply
__P((u_int32_t
, void *, size_t));
125 launch_data_t checkin_response
= NULL
;
126 launch_data_t checkin_request
= NULL
;
127 launch_data_t sockets_dict
, listening_fd_array
;
128 launch_data_t listening_fd
;
129 struct sockaddr_storage fdsockaddr
;
130 socklen_t fdsockaddrlen
= sizeof(fdsockaddr
);
137 /* check in with launchd */
138 if ((checkin_request
= launch_data_new_string(LAUNCH_KEY_CHECKIN
)) == NULL
) {
139 plog(LLV_ERROR
, LOCATION
, NULL
,
140 "failed to launch_data_new_string.\n");
143 if ((checkin_response
= launch_msg(checkin_request
)) == NULL
) {
144 plog(LLV_ERROR
, LOCATION
, NULL
,
145 "failed to launch_msg.\n");
148 if (LAUNCH_DATA_ERRNO
== launch_data_get_type(checkin_response
)) {
149 plog(LLV_ERROR
, LOCATION
, NULL
,
150 "launch_data_get_type error %d\n",
151 launch_data_get_errno(checkin_response
));
154 if ( (sockets_dict
= launch_data_dict_lookup(checkin_response
, LAUNCH_JOBKEY_SOCKETS
)) == NULL
){
155 plog(LLV_ERROR
, LOCATION
, NULL
,
156 "failed to launch_data_dict_lookup.\n");
159 if ( !(socketct
= launch_data_dict_get_count(sockets_dict
))){
160 plog(LLV_ERROR
, LOCATION
, NULL
,
161 "launch_data_dict_get_count returns no socket defined.\n");
165 if ( (listening_fd_array
= launch_data_dict_lookup(sockets_dict
, "Listeners")) == NULL
){
166 plog(LLV_ERROR
, LOCATION
, NULL
,
167 "failed to launch_data_dict_lookup.\n");
170 listenerct
= launch_data_array_get_count(listening_fd_array
);
171 for (i
= 0; i
< listenerct
; i
++) {
172 listening_fd
= launch_data_array_get_index(listening_fd_array
, i
);
173 fd
= launch_data_get_fd( listening_fd
);
174 if ( getsockname( fd
, (struct sockaddr
*)&fdsockaddr
, &fdsockaddrlen
)){
178 /* Is this the VPN control socket? */
179 if ( (((struct sockaddr
*)&fdsockaddr
)->sa_family
) == AF_UNIX
&&
180 (!(strcmp(vpncontrolsock_path
, ((struct sockaddr_un
*)&fdsockaddr
)->sun_path
))))
182 plog(LLV_INFO
, LOCATION
, NULL
,
183 "found launchd socket.\n");
188 // TODO: check if we have any leaked fd
189 if ( listenerct
== i
){
190 plog(LLV_ERROR
, LOCATION
, NULL
,
191 "failed to find launchd socket\n");
197 launch_data_free(checkin_request
);
198 if (checkin_response
)
199 launch_data_free(checkin_response
);
207 struct sockaddr_storage from
;
208 socklen_t fromlen
= sizeof(from
);
210 struct vpnctl_socket_elem
*sock_elem
;
212 sock_elem
= racoon_malloc(sizeof(struct vpnctl_socket_elem
));
213 if (sock_elem
== NULL
) {
214 plog(LLV_ERROR
, LOCATION
, NULL
,
215 "memory error: %s\n", strerror(errno
));
218 LIST_INIT(&sock_elem
->bound_addresses
);
220 sock_elem
->sock
= accept(lcconf
->sock_vpncontrol
, (struct sockaddr
*)&from
, &fromlen
);
221 if (sock_elem
->sock
< 0) {
222 plog(LLV_ERROR
, LOCATION
, NULL
,
223 "failed to accept vpn_control command: %s\n", strerror(errno
));
224 racoon_free(sock_elem
);
227 LIST_INSERT_HEAD(&lcconf
->vpnctl_comm_socks
, sock_elem
, chain
);
228 plog(LLV_NOTIFY
, LOCATION
, NULL
,
229 "accepted connection on vpn control socket.\n");
237 vpncontrol_comm_handler(struct vpnctl_socket_elem
*elem
)
239 struct vpnctl_hdr hdr
;
243 /* get buffer length */
244 while ((len
= recv(elem
->sock
, (char *)&hdr
, sizeof(hdr
), MSG_PEEK
)) < 0) {
247 plog(LLV_ERROR
, LOCATION
, NULL
,
248 "failed to recv vpn_control command: %s\n", strerror(errno
));
252 plog(LLV_DEBUG
, LOCATION
, NULL
,
253 "vpn_control socket closed by peer.\n");
254 vpncontrol_close_comm(elem
);
259 if (len
< sizeof(hdr
)) {
260 plog(LLV_ERROR
, LOCATION
, NULL
,
261 "invalid header length of vpn_control command - len=%d - expected %d\n", len
, sizeof(hdr
));
265 /* get buffer to receive */
266 if ((combuf
= racoon_malloc(ntohs(hdr
.len
) + sizeof(hdr
))) == 0) {
267 plog(LLV_ERROR
, LOCATION
, NULL
,
268 "failed to alloc buffer for vpn_control command\n");
273 while ((len
= recv(elem
->sock
, combuf
, ntohs(hdr
.len
) + sizeof(hdr
), 0)) < 0) {
276 plog(LLV_ERROR
, LOCATION
, NULL
,
277 "failed to recv vpn_control command: %s\n",
282 (void)vpncontrol_process(elem
, combuf
);
287 return 0; // return -1 only if a socket is closed
291 vpncontrol_process(struct vpnctl_socket_elem
*elem
, char *combuf
)
294 struct vpnctl_hdr
*hdr
= (struct vpnctl_hdr
*)combuf
;
296 switch (ntohs(hdr
->msg_type
)) {
298 case VPNCTL_CMD_BIND
:
300 struct vpnctl_cmd_bind
*pkt
= (struct vpnctl_cmd_bind
*)combuf
;
301 struct bound_addr
*addr
;
303 plog(LLV_DEBUG
, LOCATION
, NULL
,
304 "received bind command on vpn control socket.\n");
305 addr
= racoon_calloc(1, sizeof(struct bound_addr
));
307 plog(LLV_ERROR
, LOCATION
, NULL
,
308 "memory error: %s\n", strerror(errno
));
312 if (ntohs(pkt
->vers_len
)) {
313 addr
->version
= vmalloc(ntohs(pkt
->vers_len
));
314 if (addr
->version
== NULL
) {
315 plog(LLV_ERROR
, LOCATION
, NULL
,
316 "memory error: %s\n", strerror(errno
));
320 memcpy(addr
->version
->v
, pkt
+ 1, ntohs(pkt
->vers_len
));
322 addr
->address
= pkt
->address
;
323 LIST_INSERT_HEAD(&elem
->bound_addresses
, addr
, chain
);
324 lcconf
->auto_exit_state
|= LC_AUTOEXITSTATE_CLIENT
; /* client side */
328 case VPNCTL_CMD_UNBIND
:
330 struct vpnctl_cmd_unbind
*pkt
= (struct vpnctl_cmd_unbind
*)combuf
;
331 struct bound_addr
*addr
;
332 struct bound_addr
*t_addr
;
334 plog(LLV_DEBUG
, LOCATION
, NULL
,
335 "received unbind command on vpn control socket.\n");
336 LIST_FOREACH_SAFE(addr
, &elem
->bound_addresses
, chain
, t_addr
) {
337 if (pkt
->address
== 0xFFFFFFFF ||
338 pkt
->address
== addr
->address
) {
339 flushsainfo_dynamic(addr
->address
);
340 LIST_REMOVE(addr
, chain
);
342 vfree(addr
->version
);
349 case VPNCTL_CMD_REDIRECT
:
351 struct vpnctl_cmd_redirect
*redirect_msg
= (struct vpnctl_cmd_redirect
*)combuf
;
352 struct redirect
*raddr
;
353 struct redirect
*t_raddr
;
356 plog(LLV_DEBUG
, LOCATION
, NULL
,
357 "received redirect command on vpn control socket - address = %x.\n", ntohl(redirect_msg
->redirect_address
));
359 LIST_FOREACH_SAFE(raddr
, &lcconf
->redirect_addresses
, chain
, t_raddr
) {
360 if (raddr
->cluster_address
== redirect_msg
->address
) {
361 if (redirect_msg
->redirect_address
== 0) {
362 LIST_REMOVE(raddr
, chain
);
365 raddr
->redirect_address
= redirect_msg
->redirect_address
;
366 raddr
->force
= ntohs(redirect_msg
->force
);
373 raddr
= racoon_malloc(sizeof(struct redirect
));
375 plog(LLV_DEBUG
, LOCATION
, NULL
,
376 "cannot allcoate memory for redirect address.\n");
380 raddr
->cluster_address
= redirect_msg
->address
;
381 raddr
->redirect_address
= redirect_msg
->redirect_address
;
382 raddr
->force
= ntohs(redirect_msg
->force
);
383 LIST_INSERT_HEAD(&lcconf
->redirect_addresses
, raddr
, chain
);
389 case VPNCTL_CMD_PING
:
390 break; /* just reply for now */
392 case VPNCTL_CMD_XAUTH_INFO
:
394 struct vpnctl_cmd_xauth_info
*pkt
= (struct vpnctl_cmd_xauth_info
*)combuf
;
395 struct bound_addr
*addr
;
396 struct bound_addr
*t_addr
;
399 plog(LLV_DEBUG
, LOCATION
, NULL
,
400 "received xauth info command vpn control socket.\n");
401 LIST_FOREACH_SAFE(addr
, &elem
->bound_addresses
, chain
, t_addr
) {
402 if (pkt
->address
== addr
->address
) {
403 /* reply to the last xauth request */
405 error
= vpn_xauth_reply(pkt
->address
, attr_list
, ntohs(pkt
->hdr
.len
) - sizeof(u_int32_t
));
412 case VPNCTL_CMD_CONNECT
:
414 struct vpnctl_cmd_connect
*pkt
= (struct vpnctl_cmd_connect
*)combuf
;
415 struct bound_addr
*addr
;
416 struct bound_addr
*t_addr
;
418 plog(LLV_DEBUG
, LOCATION
, NULL
,
419 "received connect command on vpn control socket.\n");
420 LIST_FOREACH_SAFE(addr
, &elem
->bound_addresses
, chain
, t_addr
) {
421 if (pkt
->address
== addr
->address
) {
422 /* start the connection */
423 error
= vpn_connect(addr
);
430 case VPNCTL_CMD_DISCONNECT
:
432 struct vpnctl_cmd_connect
*pkt
= (struct vpnctl_cmd_connect
*)combuf
;
433 struct bound_addr
*addr
;
434 struct bound_addr
*t_addr
;
436 plog(LLV_DEBUG
, LOCATION
, NULL
,
437 "received disconnect command on vpn control socket.\n");
438 LIST_FOREACH_SAFE(addr
, &elem
->bound_addresses
, chain
, t_addr
) {
439 if (pkt
->address
== addr
->address
) {
440 /* stop the connection */
441 error
= vpn_disconnect(addr
);
448 case VPNCTL_CMD_START_PH2
:
450 struct vpnctl_cmd_start_ph2
*pkt
= (struct vpnctl_cmd_start_ph2
*)combuf
;
451 struct bound_addr
*addr
;
452 struct bound_addr
*t_addr
;
454 plog(LLV_DEBUG
, LOCATION
, NULL
,
455 "received start_ph2 command on vpn control socket.\n");
456 plogdump(LLV_DEBUG2
, pkt
, ntohs(hdr
->len
) + sizeof(struct vpnctl_hdr
));
457 LIST_FOREACH_SAFE(addr
, &elem
->bound_addresses
, chain
, t_addr
) {
458 if (pkt
->address
== addr
->address
) {
459 /* start the connection */
460 error
= vpn_start_ph2(addr
, pkt
);
467 case VPNCTL_CMD_START_DPD
:
469 struct vpnctl_cmd_start_dpd
*pkt
= (struct vpnctl_cmd_start_dpd
*)combuf
;
470 struct bound_addr
*srv
;
471 struct bound_addr
*t_addr
;
473 plog(LLV_DEBUG
, LOCATION
, NULL
,
474 "received start_dpd command on vpn control socket.\n");
475 LIST_FOREACH_SAFE(srv
, &elem
->bound_addresses
, chain
, t_addr
) {
476 if (pkt
->address
== srv
->address
) {
477 struct sockaddr_in daddr
;
479 bzero(&daddr
, sizeof(daddr
));
480 daddr
.sin_len
= sizeof(daddr
);
481 daddr
.sin_addr
.s_addr
= srv
->address
;
483 daddr
.sin_family
= AF_INET
;
486 error
= ph1_force_dpd(&daddr
);
494 plog(LLV_ERROR
, LOCATION
, NULL
,
495 "invalid command: %d\n", ntohs(hdr
->msg_type
));
496 error
= -1; // for now
501 hdr
->result
= htons(error
);
502 if (vpncontrol_reply(elem
->sock
, combuf
) < 0)
510 vpncontrol_reply(int so
, char *combuf
)
514 tlen
= send(so
, combuf
, sizeof(struct vpnctl_hdr
), 0);
516 plog(LLV_ERROR
, LOCATION
, NULL
,
517 "failed to send vpn_control message: %s\n", strerror(errno
));
525 vpncontrol_notify_need_authinfo(struct ph1handle
*iph1
, void* attr_list
, size_t attr_len
)
527 struct vpnctl_status_need_authinfo
*msg
= NULL
;
528 struct vpnctl_socket_elem
*sock_elem
;
529 struct bound_addr
*bound_addr
;
530 size_t tlen
, msg_size
;
537 plog(LLV_DEBUG
, LOCATION
, NULL
,
538 "sending vpn_control xauth need info status\n");
540 msg
= (struct vpnctl_status_need_authinfo
*)racoon_malloc(msg_size
= sizeof(struct vpnctl_status_need_authinfo
) + attr_len
);
542 plog(LLV_ERROR
, LOCATION
, NULL
,
543 "unable to allocate space for vpn control message.\n");
548 if (iph1
->remote
->sa_family
== AF_INET
)
549 address
= ((struct sockaddr_in
*)iph1
->remote
)->sin_addr
.s_addr
;
553 msg
->hdr
.cookie
= msg
->hdr
.reserved
= msg
->hdr
.result
= 0;
554 msg
->hdr
.len
= htons((msg_size
) - sizeof(struct vpnctl_hdr
));
555 if (!ike_session_is_client_ph1_rekey(iph1
)) {
556 msg
->hdr
.msg_type
= htons(VPNCTL_STATUS_NEED_AUTHINFO
);
558 msg
->hdr
.msg_type
= htons(VPNCTL_STATUS_NEED_REAUTHINFO
);
560 msg
->address
= address
;
562 memcpy(ptr
, attr_list
, attr_len
);
564 LIST_FOREACH(sock_elem
, &lcconf
->vpnctl_comm_socks
, chain
) {
565 LIST_FOREACH(bound_addr
, &sock_elem
->bound_addresses
, chain
) {
566 if (bound_addr
->address
== 0xFFFFFFFF ||
567 bound_addr
->address
== address
) {
568 plog(LLV_DEBUG
, LOCATION
, NULL
,
569 "vpn control writing %d bytes\n", msg_size
);
570 plogdump(LLV_DEBUG
, msg
, msg_size
);
571 tlen
= send(sock_elem
->sock
, msg
, msg_size
, 0);
573 plog(LLV_ERROR
, LOCATION
, NULL
,
574 "failed to send vpn_control need authinfo status: %s\n", strerror(errno
));
588 vpncontrol_notify_ike_failed(u_int16_t notify_code
, u_int16_t from
, u_int32_t address
, u_int16_t data_len
, u_int8_t
*data
)
590 struct vpnctl_status_failed
*msg
= NULL
;
591 struct vpnctl_socket_elem
*sock_elem
;
592 struct bound_addr
*bound_addr
;
595 len
= sizeof(struct vpnctl_status_failed
) + data_len
;
597 msg
= (struct vpnctl_status_failed
*)racoon_malloc(len
);
599 plog(LLV_DEBUG
, LOCATION
, NULL
,
600 "unable to allcate memory for vpn control status message.\n");
604 msg
->hdr
.msg_type
= htons(VPNCTL_STATUS_IKE_FAILED
);
605 msg
->hdr
.flags
= msg
->hdr
.cookie
= msg
->hdr
.reserved
= msg
->hdr
.result
= 0;
606 msg
->hdr
.len
= htons(len
- sizeof(struct vpnctl_hdr
));
607 msg
->address
= address
;
608 msg
->ike_code
= htons(notify_code
);
609 msg
->from
= htons(from
);
611 memcpy(msg
->data
, data
, data_len
);
612 plog(LLV_DEBUG
, LOCATION
, NULL
,
613 "sending vpn_control ike failed message - code=%d from=%s.\n", notify_code
,
614 (from
== FROM_LOCAL
? "local" : "remote"));
616 LIST_FOREACH(sock_elem
, &lcconf
->vpnctl_comm_socks
, chain
) {
617 LIST_FOREACH(bound_addr
, &sock_elem
->bound_addresses
, chain
) {
618 if (bound_addr
->address
== 0xFFFFFFFF ||
619 bound_addr
->address
== address
) {
620 tlen
= send(sock_elem
->sock
, msg
, len
, 0);
622 plog(LLV_ERROR
, LOCATION
, NULL
,
623 "unable to send vpn_control ike notify failed: %s\n", strerror(errno
));
637 vpncontrol_notify_phase_change(int start
, u_int16_t from
, struct ph1handle
*iph1
, struct ph2handle
*iph2
)
639 struct vpnctl_status_phase_change
*msg
;
640 struct vpnctl_socket_elem
*sock_elem
;
641 struct bound_addr
*bound_addr
;
642 size_t tlen
, msg_size
;
645 plog(LLV_DEBUG
, LOCATION
, NULL
,
646 "sending vpn_control phase change status\n");
648 if (iph1
&& !start
&& iph1
->mode_cfg
) {
649 if (vpn_get_config(iph1
, &msg
, &msg_size
) == 1)
650 return 0; /* mode config not finished yet */
652 msg
= racoon_malloc(msg_size
= sizeof(struct vpnctl_status_phase_change
));
657 plog(LLV_ERROR
, LOCATION
, NULL
,
658 "unable to allocate space for vpn control message.\n");
662 if (iph1
->remote
->sa_family
== AF_INET
)
663 address
= ((struct sockaddr_in
*)iph1
->remote
)->sin_addr
.s_addr
;
666 msg
->hdr
.msg_type
= htons(start
?
667 (from
== FROM_LOCAL
? VPNCTL_STATUS_PH1_START_US
: VPNCTL_STATUS_PH1_START_PEER
)
668 : VPNCTL_STATUS_PH1_ESTABLISHED
);
670 if (iph2
->dst
->sa_family
== AF_INET
)
671 address
= ((struct sockaddr_in
*)iph2
->dst
)->sin_addr
.s_addr
;
674 msg
->hdr
.msg_type
= htons(start
? VPNCTL_STATUS_PH2_START
: VPNCTL_STATUS_PH2_ESTABLISHED
);
676 msg
->hdr
.cookie
= msg
->hdr
.reserved
= msg
->hdr
.result
= 0;
677 msg
->hdr
.len
= htons((msg_size
) - sizeof(struct vpnctl_hdr
));
678 msg
->address
= address
;
680 LIST_FOREACH(sock_elem
, &lcconf
->vpnctl_comm_socks
, chain
) {
681 LIST_FOREACH(bound_addr
, &sock_elem
->bound_addresses
, chain
) {
682 if (bound_addr
->address
== 0xFFFFFFFF ||
683 bound_addr
->address
== address
) {
684 plog(LLV_DEBUG
, LOCATION
, NULL
,
685 "vpn control writing %d bytes\n", msg_size
);
686 plogdump(LLV_DEBUG
, msg
, msg_size
);
687 tlen
= send(sock_elem
->sock
, msg
, msg_size
, 0);
689 plog(LLV_ERROR
, LOCATION
, NULL
,
690 "failed to send vpn_control phase change status: %s\n", strerror(errno
));
707 if (vpncontrolsock_path
== NULL
) {
708 lcconf
->sock_vpncontrol
= -1;
712 if ( (lcconf
->sock_vpncontrol
= checklaunchd()) ){
717 memset(&sunaddr
, 0, sizeof(sunaddr
));
718 sunaddr
.sun_family
= AF_UNIX
;
719 snprintf(sunaddr
.sun_path
, sizeof(sunaddr
.sun_path
),
720 "%s", vpncontrolsock_path
);
722 lcconf
->sock_vpncontrol
= socket(AF_UNIX
, SOCK_STREAM
, 0);
723 if (lcconf
->sock_vpncontrol
== -1) {
724 plog(LLV_ERROR
, LOCATION
, NULL
,
725 "socket: %s\n", strerror(errno
));
729 unlink(sunaddr
.sun_path
);
730 if (bind(lcconf
->sock_vpncontrol
, (struct sockaddr
*)&sunaddr
,
731 sizeof(sunaddr
)) != 0) {
732 plog(LLV_ERROR
, LOCATION
, NULL
,
733 "bind(sockname:%s): %s\n",
734 sunaddr
.sun_path
, strerror(errno
));
735 (void)close(lcconf
->sock_vpncontrol
);
739 if (chown(sunaddr
.sun_path
, vpncontrolsock_owner
, vpncontrolsock_group
) != 0) {
740 plog(LLV_ERROR
, LOCATION
, NULL
,
741 "chown(%s, %d, %d): %s\n",
742 sunaddr
.sun_path
, vpncontrolsock_owner
,
743 vpncontrolsock_group
, strerror(errno
));
744 (void)close(lcconf
->sock_vpncontrol
);
748 if (chmod(sunaddr
.sun_path
, vpncontrolsock_mode
) != 0) {
749 plog(LLV_ERROR
, LOCATION
, NULL
,
750 "chmod(%s, 0%03o): %s\n",
751 sunaddr
.sun_path
, vpncontrolsock_mode
, strerror(errno
));
752 (void)close(lcconf
->sock_vpncontrol
);
756 if (listen(lcconf
->sock_vpncontrol
, 5) != 0) {
757 plog(LLV_ERROR
, LOCATION
, NULL
,
758 "listen(sockname:%s): %s\n",
759 sunaddr
.sun_path
, strerror(errno
));
760 (void)close(lcconf
->sock_vpncontrol
);
763 plog(LLV_DEBUG
, LOCATION
, NULL
,
764 "opened %s as racoon management.\n", sunaddr
.sun_path
);
774 struct vpnctl_socket_elem
*elem
;
775 struct vpnctl_socket_elem
*t_elem
;
777 plog(LLV_DEBUG
, LOCATION
, NULL
,
778 "vpncontrol_close.\n");
780 if (lcconf
->sock_vpncontrol
!= -1) {
781 close(lcconf
->sock_vpncontrol
);
782 lcconf
->sock_vpncontrol
= -1;
784 LIST_FOREACH_SAFE(elem
, &lcconf
->vpnctl_comm_socks
, chain
, t_elem
)
785 vpncontrol_close_comm(elem
);
790 vpncontrol_close_comm(struct vpnctl_socket_elem
*elem
)
792 struct bound_addr
*addr
;
793 struct bound_addr
*t_addr
;
795 plog(LLV_DEBUG
, LOCATION
, NULL
,
796 "vpncontrol_close_comm.\n");
798 LIST_REMOVE(elem
, chain
);
799 if (elem
->sock
!= -1)
801 LIST_FOREACH_SAFE(addr
, &elem
->bound_addresses
, chain
, t_addr
) {
802 flushsainfo_dynamic(addr
->address
);
803 LIST_REMOVE(addr
, chain
);
805 vfree(addr
->version
);
813 vpn_control_connected(void)
815 if (LIST_EMPTY(&lcconf
->vpnctl_comm_socks
))