Commit 8f432528 authored by Kevin Cernekee's avatar Kevin Cernekee Committed by David Woodhouse

library: Add setup_tun() callback

Library users used to do something like:

  openconnect_obtain_cookie()
  openconnect_make_cstp_connection()
  openconnect_setup_dtls()
  openconnect_get_ip_info()
  # ask the OS to create the tun interface
  openconnect_setup_tun_fd()
  openconnect_mainloop()

But now that MTU is calculated a few seconds after the mainloop starts
up, it is necessary to provide a callback so that the calling application
can create a tun interface with the correct MTU.

(Bonus: Android and Chrome OS currently do not allow the MTU, IP address,
or other parameters to be adjusted after the initial settings were sent
to the OS.)
Signed-off-by: default avatarKevin Cernekee <cernekee@gmail.com>
Signed-off-by: default avatarDavid Woodhouse <David.Woodhouse@intel.com>
parent 6537e122
......@@ -163,6 +163,14 @@ public final class LibTest {
break;
}
}
@Override
public void onSetupTun() {
System.out.println("SETUP_TUN");
if (setupTunDevice("/etc/vpnc/vpnc-script", null) != 0 &&
setupTunScript("ocproxy") != 0)
die("Error setting up tunnel");
}
}
private static void printList(String pfx, List<String> ss) {
......@@ -233,10 +241,6 @@ public final class LibTest {
printIPInfo(lib.getIPInfo());
if (lib.setupTunDevice("/etc/vpnc/vpnc-script", null) != 0 &&
lib.setupTunScript("ocproxy") != 0)
die("Error setting up tunnel");
if (lib.setupDTLS(60) != 0)
die("Error setting up DTLS");
......
......@@ -62,6 +62,7 @@ public abstract class LibOpenConnect {
public void onStatsUpdate(VPNStats stats) { }
public int onTokenLock() { return 0; }
public int onTokenUnlock(String newToken) { return 0; }
public void onSetupTun() { }
/* create/destroy library instances */
......
......@@ -294,6 +294,21 @@ out:
(*ctx->jenv)->PopLocalFrame(ctx->jenv, NULL);
}
static void setup_tun_cb(void *privdata)
{
struct libctx *ctx = privdata;
jmethodID mid;
if ((*ctx->jenv)->PushLocalFrame(ctx->jenv, 256) < 0)
return;
mid = get_obj_mid(ctx, ctx->jobj, "onSetupTun", "()V");
if (mid)
(*ctx->jenv)->CallVoidMethod(ctx->jenv, ctx->jobj, mid);
(*ctx->jenv)->PopLocalFrame(ctx->jenv, NULL);
}
static jobject new_auth_form(struct libctx *ctx, struct oc_auth_form *form)
{
jmethodID mid;
......@@ -609,6 +624,7 @@ JNIEXPORT jlong JNICALL Java_org_infradead_libopenconnect_LibOpenConnect_init(
unlock_token_cb);
openconnect_set_protect_socket_handler(ctx->vpninfo, protect_socket_cb);
openconnect_set_stats_handler(ctx->vpninfo, stats_cb);
openconnect_set_setup_tun_handler(ctx->vpninfo, setup_tun_cb);
ctx->cmd_fd = openconnect_setup_cmd_pipe(ctx->vpninfo);
if (ctx->cmd_fd < 0)
......
......@@ -43,6 +43,7 @@ OPENCONNECT_5.0 {
openconnect_set_proxy_auth;
openconnect_set_reported_os;
openconnect_set_reqmtu;
openconnect_set_setup_tun_handler;
openconnect_set_stats_handler;
openconnect_set_stoken_mode;
openconnect_set_system_trust;
......
......@@ -780,6 +780,12 @@ void openconnect_override_getaddrinfo(struct openconnect_info *vpninfo, openconn
vpninfo->getaddrinfo_override = gai_fn;
}
void openconnect_set_setup_tun_handler(struct openconnect_info *vpninfo,
openconnect_setup_tun_vfn setup_tun)
{
vpninfo->setup_tun = setup_tun;
}
void openconnect_set_stats_handler(struct openconnect_info *vpninfo,
openconnect_stats_vfn stats_handler)
{
......
......@@ -110,6 +110,12 @@ static int setup_tun_device(struct openconnect_info *vpninfo)
{
int ret;
if (vpninfo->setup_tun) {
vpninfo->setup_tun(vpninfo->cbdata);
if (tun_is_up(vpninfo))
return 0;
}
#ifndef _WIN32
if (vpninfo->use_tun_script) {
ret = openconnect_setup_tun_script(vpninfo, vpninfo->vpnc_script);
......
......@@ -610,6 +610,7 @@ struct openconnect_info {
openconnect_progress_vfn progress;
openconnect_protect_socket_vfn protect_socket;
openconnect_getaddrinfo_vfn getaddrinfo_override;
openconnect_setup_tun_vfn setup_tun;
int (*ssl_read)(struct openconnect_info *vpninfo, char *buf, size_t len);
int (*ssl_gets)(struct openconnect_info *vpninfo, char *buf, size_t len);
......
......@@ -43,6 +43,7 @@ extern "C" {
* - Add openconnect_get_dtls_compression().
* - Add openconnect_disable_ipv6().
* - Add ip_info->gateway_addr.
* - Add openconnect_set_setup_tun_handler().
*
* API version 5.2 (v7.05; 2015-03-10):
* - Add openconnect_set_http_auth(), openconnect_set_protocol().
......@@ -609,6 +610,11 @@ typedef int (*openconnect_getaddrinfo_vfn) (void *privdata, const char *node, co
const struct addrinfo *hints, struct addrinfo **res);
void openconnect_override_getaddrinfo(struct openconnect_info *vpninfo, openconnect_getaddrinfo_vfn gai_fn);
/* Callback for configuring the interface after MTU detection finishes. */
typedef void (*openconnect_setup_tun_vfn) (void *privdata);
void openconnect_set_setup_tun_handler(struct openconnect_info *vpninfo,
openconnect_setup_tun_vfn setup_tun);
#ifdef __cplusplus
}
#endif
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment