Update translations from Transifex
[platform/upstream/openconnect.git] / tun.c
diff --git a/tun.c b/tun.c
index 05f33e4..a28f9d4 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)
 {
+       int ret;
+
        if (!vpninfo->vpnc_script)
                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;
 }
 
@@ -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,
@@ -511,7 +582,7 @@ static int os_setup_tun(struct openconnect_info *vpninfo)
                }
                snprintf(tun_name, sizeof(tun_name),
                         "/dev/%s", vpninfo->ifname);
-               tun_fd = open(tun_name, O_RDWR);
+               tun_fd = bsd_open_tun(tun_name);
                if (tun_fd < 0) {
                        int err = errno;
                        vpn_progress(vpninfo, PRG_ERR,
@@ -519,10 +590,27 @@ static int os_setup_tun(struct openconnect_info *vpninfo)
                                     tun_name, strerror(err));
                        return -EINVAL;
                }
-       } else {
+       }
+#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) {
+               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 = open(tun_name, O_RDWR);
+                       tun_fd = bsd_open_tun(tun_name);
                        if (tun_fd >= 0)
                                break;
                }
@@ -615,7 +703,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);