/*
* OpenConnect (SSL + DTLS) VPN client
*
- * Copyright © 2008-2010 Intel Corporation.
+ * Copyright © 2008-2012 Intel Corporation.
*
* Author: David Woodhouse <dwmw2@infradead.org>
*
#include <sys/stat.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
+#include <sys/wait.h>
#include <string.h>
#include <signal.h>
#include <fcntl.h>
#include <arpa/inet.h>
#include <errno.h>
#include <ctype.h>
+#include <stdio.h>
+#include <stdlib.h>
#if defined(__sun__)
#include <stropts.h>
#include <sys/sockio.h>
memset(&ifr, 0, sizeof(ifr));
strncpy(ifr.ifr_name, vpninfo->ifname, sizeof(ifr.ifr_name) - 1);
- ifr.ifr_mtu = vpninfo->mtu;
+ ifr.ifr_mtu = vpninfo->actual_mtu;
if (ioctl(net_fd, SIOCSIFMTU, &ifr) < 0)
perror(_("SIOCSIFMTU"));
char *banner, *q;
const char *p;
- if (!vpninfo->banner || !(banner = malloc(strlen(vpninfo->banner)))) {
+ if (!vpninfo->banner || !(banner = malloc(strlen(vpninfo->banner)+1))) {
unsetenv("CISCO_BANNER");
return;
}
unsetenv("CISCO_SPLIT_INC");
unsetenv("CISCO_SPLIT_EXC");
- setenv_int("INTERNAL_IP4_MTU", vpninfo->mtu);
+ setenv_int("INTERNAL_IP4_MTU", vpninfo->actual_mtu);
if (vpninfo->vpn_addr) {
setenv("INTERNAL_IP4_ADDRESS", vpninfo->vpn_addr, 1);
if (vpninfo->vpn_proxy_pac)
setenv("CISCO_PROXY_PAC", vpninfo->vpn_proxy_pac, 1);
+ if (vpninfo->split_dns) {
+ char *list;
+ int len = 0;
+ struct split_include *dns = vpninfo->split_dns;
+
+ while (dns) {
+ len += strlen(dns->route) + 1;
+ dns = dns->next;
+ }
+ list = malloc(len);
+ if (list) {
+ char *p = list;
+
+ dns = vpninfo->split_dns;
+ while (1) {
+ strcpy(p, dns->route);
+ p += strlen(p);
+ dns = dns->next;
+ if (!dns)
+ break;
+ *(p++) = ',';
+ }
+ setenv("CISCO_SPLIT_DNS", list, 1);
+ free(list);
+ }
+ }
if (vpninfo->split_includes) {
struct split_include *this = vpninfo->split_includes;
int nr_split_includes = 0;
int script_config_tun(struct openconnect_info *vpninfo, const char *reason)
{
- if (!vpninfo->vpnc_script)
+ int ret;
+
+ if (!vpninfo->vpnc_script || vpninfo->script_tun)
return 0;
setenv("reason", reason, 1);
- if (system(vpninfo->vpnc_script)) {
+ ret = system(vpninfo->vpnc_script);
+ if (ret == -1) {
int e = errno;
vpn_progress(vpninfo, PRG_ERR,
_("Failed to spawn script '%s' for %s: %s\n"),
vpninfo->vpnc_script, reason, strerror(e));
return -e;
}
+ if (!WIFEXITED(ret)) {
+ vpn_progress(vpninfo, PRG_ERR,
+ _("Script '%s' exited abnormally (%x)\n"),
+ vpninfo->vpnc_script, ret);
+ return -EIO;
+ }
+ ret = WEXITSTATUS(ret);
+ if (ret) {
+ vpn_progress(vpninfo, PRG_ERR,
+ _("Script '%s' returned error %d\n"),
+ vpninfo->vpnc_script, ret);
+ return -EIO;
+ }
return 0;
}
tun2_fd = open("/dev/tun", O_RDWR);
if (tun2_fd < 0) {
- perror(_("Could not /dev/tun for plumbing"));
+ perror(_("Could not open /dev/tun for plumbing"));
return -EIO;
}
if (ioctl(tun2_fd, I_PUSH, "ip") < 0) {
}
#endif
+#ifdef SIOCIFCREATE
+static int bsd_open_tun(char *tun_name)
+{
+ int fd;
+ int s;
+ struct ifreq ifr;
+
+ fd = open(tun_name, O_RDWR);
+ if (fd >= 0) {
+ return fd;
+
+ s = socket(AF_INET, SOCK_DGRAM, 0);
+ if (s < 0)
+ return -1;
+
+ memset(&ifr, 0, sizeof(ifr));
+ strncpy(ifr.ifr_name, tun_name + 5, sizeof(ifr.ifr_name) - 1);
+ if (!ioctl(s, SIOCIFCREATE, &ifr))
+ fd = open(tun_name, O_RDWR);
+
+ close(s);
+ }
+ return fd;
+}
+#else
+#define bsd_open_tun(tun_name) open(tun_name, O_RDWR)
+#endif
+
static int os_setup_tun(struct openconnect_info *vpninfo)
{
- int tun_fd;
+ int tun_fd = -1;
#ifdef IFF_TUN /* Linux */
struct ifreq ifr;
if (tun_fd < 0) {
/* If the error on /dev/tun is ENOENT, that's boring.
Use the error we got on /dev/net/tun instead */
- if (errno != -ENOENT)
+ if (errno != ENOENT)
tunerr = errno;
vpn_progress(vpninfo, PRG_ERR,
#else /* BSD et al have /dev/tun$x devices */
static char tun_name[80];
int i;
- for (i = 0; i < 255; i++) {
- sprintf(tun_name, "/dev/tun%d", i);
- tun_fd = open(tun_name, O_RDWR);
- if (tun_fd >= 0)
- break;
+
+ if (vpninfo->ifname) {
+ char *endp = NULL;
+ if (strncmp(vpninfo->ifname, "tun", 3) ||
+ ((void)strtol(vpninfo->ifname + 3, &endp, 10), !endp) ||
+ *endp) {
+ vpn_progress(vpninfo, PRG_ERR,
+ _("Invalid interface name '%s'; must match 'tun%%d'\n"),
+ vpninfo->ifname);
+ return -EINVAL;
+ }
+ snprintf(tun_name, sizeof(tun_name),
+ "/dev/%s", vpninfo->ifname);
+ tun_fd = bsd_open_tun(tun_name);
+ if (tun_fd < 0) {
+ int err = errno;
+ vpn_progress(vpninfo, PRG_ERR,
+ _("Cannot open '%s': %s\n"),
+ tun_name, strerror(err));
+ return -EINVAL;
+ }
}
+#ifdef HAVE_FDEVNAME_R
+ /* We don't have to iterate over the possible devices; on FreeBSD
+ at least, opening /dev/tun will give us the next available
+ device. */
if (tun_fd < 0) {
- perror(_("open tun"));
- exit(1);
+ tun_fd = open("/dev/tun", O_RDWR);
+ if (tun_fd >= 0) {
+ if (!fdevname_r(tun_fd, tun_name, sizeof(tun_name)) ||
+ strncmp(tun_name, "tun", 3)) {
+ close(tun_fd);
+ tun_fd = -1;
+ } else
+ vpninfo->ifname = strdup(tun_name);
+ }
+ }
+#endif
+ if (tun_fd < 0) {
+ for (i = 0; i < 255; i++) {
+ sprintf(tun_name, "/dev/tun%d", i);
+ tun_fd = bsd_open_tun(tun_name);
+ if (tun_fd >= 0)
+ break;
+ }
+ if (tun_fd < 0) {
+ perror(_("open tun"));
+ exit(1);
+ }
+ vpninfo->ifname = strdup(tun_name + 5);
}
- vpninfo->ifname = strdup(tun_name + 5);
#ifdef TUNSIFHEAD
i = 1;
if (ioctl(tun_fd, TUNSIFHEAD, &i) < 0) {
perror(_("fork"));
exit(1);
} else if (!child) {
+ if (setpgid(0, getpid()) < 0)
+ perror(_("setpgid"));
close(tun_fd);
setenv_int("VPNFD", fds[1]);
execl("/bin/sh", "/bin/sh", "-c", vpninfo->vpnc_script, NULL);
if (FD_ISSET(vpninfo->tun_fd, &vpninfo->select_rfds)) {
while (1) {
- int len = vpninfo->mtu;
+ int len = vpninfo->actual_mtu;
if (!out_pkt) {
out_pkt = malloc(sizeof(struct pkt) + len);
void shutdown_tun(struct openconnect_info *vpninfo)
{
if (vpninfo->script_tun) {
- kill(vpninfo->script_tun, SIGHUP);
+ /* nuke the whole process group */
+ kill(-vpninfo->script_tun, SIGHUP);
} else {
script_config_tun(vpninfo, "disconnect");
#ifdef __sun__