Add packaging
[platform/upstream/openconnect.git] / tun.c
diff --git a/tun.c b/tun.c
index 09cf635..5c04177 100644 (file)
--- a/tun.c
+++ b/tun.c
@@ -26,6 +26,7 @@
 #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>
@@ -91,7 +92,7 @@ static int set_tun_mtu(struct openconnect_info *vpninfo)
 
        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"));
@@ -259,7 +260,7 @@ static void set_script_env(struct openconnect_info *vpninfo)
        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);
@@ -310,6 +311,32 @@ static void set_script_env(struct openconnect_info *vpninfo)
        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;
@@ -347,17 +374,33 @@ static void set_script_env(struct openconnect_info *vpninfo)
 
 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;
 }
 
@@ -369,7 +412,7 @@ static int link_proto(int unit_nr, const char *devname, uint64_t flags)
 
        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) {
@@ -412,9 +455,37 @@ static int link_proto(int unit_nr, const char *devname, uint64_t flags)
 }
 #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;
@@ -431,7 +502,7 @@ static int os_setup_tun(struct openconnect_info *vpninfo)
        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,
@@ -498,17 +569,57 @@ static int os_setup_tun(struct openconnect_info *vpninfo)
 #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) {
@@ -541,6 +652,8 @@ int setup_tun(struct openconnect_info *vpninfo)
                        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);
@@ -592,7 +705,7 @@ int tun_mainloop(struct openconnect_info *vpninfo, int *timeout)
 
        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);
@@ -675,7 +788,8 @@ int tun_mainloop(struct openconnect_info *vpninfo, int *timeout)
 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__