mirror of
https://github.com/yuezk/GlobalProtect-openconnect.git
synced 2026-11-19 14:36:15 -05:00
fix: retain VPN cancellation throughout connection startup
This commit is contained in:
5 files changed
+298
-53
No files matched your search
@@ -53,24 +53,38 @@ pub(crate) struct VpnSessionInfoRaw {
|
||||
|
||||
#[link(name = "vpn")]
|
||||
unsafe extern "C" {
|
||||
fn vpn_write_cancel(fd: c_int) -> c_int;
|
||||
#[link_name = "vpn_connect"]
|
||||
fn vpn_connect(
|
||||
options: *const ConnectOptions,
|
||||
callback: extern "C" fn(i32, *const VpnSessionInfoRaw, *mut c_void),
|
||||
) -> c_int;
|
||||
|
||||
#[link_name = "vpn_disconnect"]
|
||||
fn vpn_disconnect();
|
||||
}
|
||||
|
||||
pub(crate) fn write_cancel(fd: i32) -> std::io::Result<()> {
|
||||
let error = unsafe { vpn_write_cancel(fd) };
|
||||
if error == 0 {
|
||||
Ok(())
|
||||
} else {
|
||||
Err(std::io::Error::from_raw_os_error(error))
|
||||
}
|
||||
}
|
||||
|
||||
#[unsafe(no_mangle)]
|
||||
extern "C" fn vpn_attach_command_pipe(vpn: *mut c_void, fd: c_int) -> c_int {
|
||||
unsafe { &*(vpn as *const Vpn) }.attach_command_pipe(fd).into()
|
||||
}
|
||||
|
||||
#[unsafe(no_mangle)]
|
||||
extern "C" fn vpn_detach_command_pipe(vpn: *mut c_void) {
|
||||
unsafe { &*(vpn as *const Vpn) }.detach_command_pipe();
|
||||
}
|
||||
|
||||
pub(crate) fn connect(options: &ConnectOptions) -> i32 {
|
||||
unsafe { vpn_connect(options, on_vpn_connected) }
|
||||
}
|
||||
|
||||
pub(crate) fn disconnect() {
|
||||
unsafe { vpn_disconnect() }
|
||||
}
|
||||
|
||||
#[unsafe(no_mangle)]
|
||||
extern "C" fn on_vpn_connected(pipe_fd: i32, session_info: *const VpnSessionInfoRaw, vpn: *mut c_void) {
|
||||
let vpn = unsafe { &*(vpn as *const Vpn) };
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
#include <openconnect.h>
|
||||
#include <errno.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
@@ -7,13 +8,12 @@
|
||||
|
||||
#include "vpn.h"
|
||||
|
||||
void *g_user_data;
|
||||
|
||||
static int g_cmd_pipe_fd;
|
||||
static const char *g_vpnc_script;
|
||||
static const char *g_vpnc_interface;
|
||||
static int g_script_tun;
|
||||
static vpn_connected_callback on_vpn_connected;
|
||||
struct vpn_context {
|
||||
struct openconnect_info *vpninfo;
|
||||
int command_fd;
|
||||
const vpn_options *options;
|
||||
vpn_connected_callback on_connected;
|
||||
};
|
||||
|
||||
/* Validate the peer certificate */
|
||||
static int validate_peer_cert(__attribute__((unused)) void *_vpninfo,
|
||||
@@ -40,14 +40,17 @@ static void print_progress(__attribute__((unused)) void *_vpninfo, int level,
|
||||
}
|
||||
}
|
||||
|
||||
static void setup_tun_handler(void *_vpninfo)
|
||||
static void setup_tun_handler(void *private_data)
|
||||
{
|
||||
struct vpn_context *context = private_data;
|
||||
struct openconnect_info *_vpninfo = context->vpninfo;
|
||||
const vpn_options *options = context->options;
|
||||
int ret;
|
||||
if (g_script_tun) {
|
||||
ret = openconnect_setup_tun_script(_vpninfo, g_vpnc_script);
|
||||
if (options->script_tun) {
|
||||
ret = openconnect_setup_tun_script(_vpninfo, options->script);
|
||||
} else {
|
||||
ret = openconnect_setup_tun_device(_vpninfo, g_vpnc_script,
|
||||
g_vpnc_interface);
|
||||
ret = openconnect_setup_tun_device(_vpninfo, options->script,
|
||||
options->interface);
|
||||
}
|
||||
|
||||
if (!ret) {
|
||||
@@ -63,7 +66,7 @@ static void setup_tun_handler(void *_vpninfo)
|
||||
.nlb_connected_gw_ip =
|
||||
openconnect_get_gp_nlb_connected_gw_ip(_vpninfo),
|
||||
};
|
||||
on_vpn_connected(g_cmd_pipe_fd, &session_info, g_user_data);
|
||||
context->on_connected(context->command_fd, &session_info, options->user_data);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -74,18 +77,15 @@ int vpn_connect(const vpn_options *options, vpn_connected_callback callback)
|
||||
struct utsname utsbuf;
|
||||
const char *effective_local_hostname = NULL;
|
||||
|
||||
g_user_data = options->user_data;
|
||||
g_vpnc_script = options->script;
|
||||
g_vpnc_interface = options->interface;
|
||||
g_script_tun = options->script_tun;
|
||||
on_vpn_connected = callback;
|
||||
struct vpn_context context = { .options = options, .on_connected = callback };
|
||||
int result = 1;
|
||||
|
||||
INFO("USER_AGENT: %s", options->user_agent);
|
||||
INFO("OS: %s", options->os);
|
||||
INFO("CLIENT_VERSION: %s", options->client_version);
|
||||
INFO("HOST_ID: %s", options->host_id ? options->host_id : "(not set)");
|
||||
INFO("VPNC_SCRIPT: %s", options->script);
|
||||
INFO("SCRIPT_TUN: %d", g_script_tun);
|
||||
INFO("SCRIPT_TUN: %d", options->script_tun);
|
||||
INFO("CSD_USER: %d", options->csd_uid);
|
||||
INFO("CSD_WRAPPER: %s", options->csd_wrapper);
|
||||
INFO("RECONNECT_TIMEOUT: %d", options->reconnect_timeout);
|
||||
@@ -97,13 +97,26 @@ int vpn_connect(const vpn_options *options, vpn_connected_callback callback)
|
||||
|
||||
vpninfo =
|
||||
openconnect_vpninfo_new(options->user_agent, validate_peer_cert,
|
||||
NULL, NULL, print_progress, NULL);
|
||||
NULL, NULL, print_progress, &context);
|
||||
|
||||
if (!vpninfo) {
|
||||
ERROR("openconnect_vpninfo_new failed");
|
||||
return 1;
|
||||
}
|
||||
|
||||
context.vpninfo = vpninfo;
|
||||
context.command_fd = openconnect_setup_cmd_pipe(vpninfo);
|
||||
if (context.command_fd < 0) {
|
||||
ERROR("openconnect_setup_cmd_pipe failed");
|
||||
goto cleanup;
|
||||
}
|
||||
/* An earlier disconnect remains latched on this attempt. */
|
||||
if (vpn_attach_command_pipe(options->user_data, context.command_fd)) {
|
||||
INFO("VPN attempt canceled before startup");
|
||||
result = -EINTR;
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
openconnect_set_loglevel(vpninfo, PRG_TRACE);
|
||||
openconnect_init_ssl();
|
||||
openconnect_set_protocol(vpninfo, "gp");
|
||||
@@ -170,23 +183,15 @@ int vpn_connect(const vpn_options *options, vpn_connected_callback callback)
|
||||
if (options->dpd_interval > 0) {
|
||||
openconnect_set_dpd(vpninfo, options->dpd_interval);
|
||||
}
|
||||
|
||||
g_cmd_pipe_fd = openconnect_setup_cmd_pipe(vpninfo);
|
||||
if (g_cmd_pipe_fd < 0) {
|
||||
ERROR("openconnect_setup_cmd_pipe failed");
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Essential step
|
||||
if (openconnect_make_cstp_connection(vpninfo) != 0) {
|
||||
ERROR("openconnect_make_cstp_connection failed");
|
||||
return 1;
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
if (options->no_dtls || openconnect_setup_dtls(vpninfo, 60) != 0) {
|
||||
openconnect_disable_dtls(vpninfo);
|
||||
}
|
||||
|
||||
// Essential step
|
||||
openconnect_set_setup_tun_handler(vpninfo, setup_tun_handler);
|
||||
|
||||
@@ -196,23 +201,25 @@ int vpn_connect(const vpn_options *options, vpn_connected_callback callback)
|
||||
|
||||
if (ret) {
|
||||
INFO("openconnect_mainloop returned %d, exiting", ret);
|
||||
openconnect_vpninfo_free(vpninfo);
|
||||
return ret;
|
||||
result = ret;
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
INFO("openconnect_mainloop returned 0, reconnecting");
|
||||
}
|
||||
cleanup:
|
||||
vpn_detach_command_pipe(options->user_data);
|
||||
openconnect_vpninfo_free(vpninfo);
|
||||
return result;
|
||||
}
|
||||
|
||||
/* Stop the VPN connection */
|
||||
void vpn_disconnect()
|
||||
/* Called with the attempt cancellation mutex held, never after detach/free. */
|
||||
int vpn_write_cancel(int fd)
|
||||
{
|
||||
char cmd = OC_CMD_CANCEL;
|
||||
|
||||
INFO("Stopping VPN connection: %d", g_cmd_pipe_fd);
|
||||
|
||||
if (write(g_cmd_pipe_fd, &cmd, 1) < 0) {
|
||||
ERROR("Failed to write to command pipe, VPN connection may not "
|
||||
"be stopped");
|
||||
}
|
||||
char command = OC_CMD_CANCEL;
|
||||
ssize_t written;
|
||||
do {
|
||||
written = write(fd, &command, 1);
|
||||
} while (written < 0 && errno == EINTR);
|
||||
return written == 1 ? 0 : (written < 0 ? errno : EIO);
|
||||
}
|
||||
@@ -52,7 +52,9 @@ typedef struct vpn_options {
|
||||
} vpn_options;
|
||||
|
||||
int vpn_connect(const vpn_options *options, vpn_connected_callback callback);
|
||||
void vpn_disconnect();
|
||||
|
||||
extern int vpn_attach_command_pipe(void *user_data, int fd);
|
||||
extern void vpn_detach_command_pipe(void *user_data);
|
||||
|
||||
extern void vpn_log(int level, const char *msg);
|
||||
|
||||
|
||||
Reference in new issue
Block a user