Add packaging
[platform/upstream/openconnect.git] / tun.c
diff --git a/tun.c b/tun.c
index a9e18ac..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);
@@ -373,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;
 }
 
@@ -395,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) {
@@ -635,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);
@@ -686,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);
@@ -769,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__