Handle 'script' going away
[platform/upstream/openconnect.git] / tun.c
1 /*
2  * Open AnyConnect (SSL + DTLS) client
3  *
4  * © 2008 David Woodhouse <dwmw2@infradead.org>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public License
8  * as published by the Free Software Foundation; either version 2.1 of
9  * the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to:
18  *
19  *   Free Software Foundation, Inc.
20  *   51 Franklin Street, Fifth Floor,
21  *   Boston, MA 02110-1301 USA
22  */
23
24 #include <string.h>
25 #include <net/if.h>
26 #include <sys/ioctl.h>
27 #include <linux/if_tun.h>
28 #include <sys/types.h>
29 #include <sys/stat.h>
30 #include <fcntl.h>
31 #include <unistd.h>
32 #include <netinet/in.h>
33 #include <arpa/inet.h>
34 #include <errno.h>
35
36 #include "openconnect.h"
37
38 static int local_config_tun(struct openconnect_info *vpninfo, int mtu_only)
39 {
40         struct ifreq ifr;
41         int net_fd;
42
43         net_fd = socket(PF_INET, SOCK_DGRAM, 0);
44         if (net_fd < 0) {
45                 perror("open net");
46                 return -EINVAL;
47         }
48         memset(&ifr, 0, sizeof(ifr));
49         strncpy(ifr.ifr_name, vpninfo->ifname, sizeof(ifr.ifr_name) - 1);
50
51         if (!mtu_only) {
52                 struct sockaddr_in *addr = (struct sockaddr_in *) &ifr.ifr_addr;
53
54                 if (ioctl(net_fd, SIOCGIFFLAGS, &ifr) < 0)
55                         perror("SIOCGIFFLAGS");
56
57                 ifr.ifr_flags |= IFF_UP | IFF_POINTOPOINT; 
58                 if (ioctl(net_fd, SIOCSIFFLAGS, &ifr) < 0)
59                         perror("SIOCSIFFLAGS");
60
61                 addr->sin_family = AF_INET;
62                 addr->sin_addr.s_addr = inet_addr(vpninfo->vpn_addr);
63                 if (ioctl(net_fd, SIOCSIFADDR, &ifr) < 0)
64                         perror("SIOCSIFADDR");
65         }
66
67         ifr.ifr_mtu = vpninfo->mtu;
68         if (ioctl(net_fd, SIOCSIFMTU, &ifr) < 0)
69                 perror("SIOCSIFMTU");
70
71         close(net_fd);
72
73         return 0;
74 }
75
76 static int setenv_int(const char *opt, int value)
77 {
78         char buf[16];
79         sprintf(buf, "%d", value);
80         return setenv(opt, buf, 1);
81 }
82
83 static int appendenv(const char *opt, const char *new)
84 {
85         char buf[1024];
86         char *old = getenv(opt);
87
88         buf[1023] = 0;
89         if (old)
90                 snprintf(buf, 1023, "%s %s", old, new);
91         else
92                 snprintf(buf, 1023, "%s", new);
93
94         return setenv(opt, buf, 1);
95 }
96
97 static void set_script_env(struct openconnect_info *vpninfo)
98 {
99         struct sockaddr_in *sin = (void *)vpninfo->peer_addr;
100
101         setenv("VPNGATEWAY", inet_ntoa(sin->sin_addr), 1);
102         setenv("TUNDEV", vpninfo->ifname, 1);
103         setenv("reason", "connect", 1);
104         unsetenv("CISCO_BANNER");
105         unsetenv("CISCO_SPLIT_INC");
106
107         setenv_int("INTERNAL_IP4_MTU", vpninfo->mtu);
108
109         setenv("INTERNAL_IP4_ADDRESS", vpninfo->vpn_addr, 1);
110         setenv("INTERNAL_IP4_NETMASK", vpninfo->vpn_netmask, 1);
111         
112         if (vpninfo->vpn_dns[0])
113                 setenv("INTERNAL_IP4_DNS", vpninfo->vpn_dns[0], 1);
114         else
115                 unsetenv("INTERNAL_IP4_DNS");
116         if (vpninfo->vpn_dns[1])
117                 appendenv("INTERNAL_IP4_DNS", vpninfo->vpn_dns[1]);
118         if (vpninfo->vpn_dns[2])
119                 appendenv("INTERNAL_IP4_DNS", vpninfo->vpn_dns[2]);
120
121         if (vpninfo->vpn_nbns[0])
122                 setenv("INTERNAL_IP4_NBNS", vpninfo->vpn_nbns[0], 1);
123         else
124                 unsetenv("INTERNAL_IP4_NBNS");
125         if (vpninfo->vpn_nbns[1])
126                 appendenv("INTERNAL_IP4_NBNS", vpninfo->vpn_nbns[1]);
127         if (vpninfo->vpn_nbns[2])
128                 appendenv("INTERNAL_IP4_NBNS", vpninfo->vpn_nbns[2]);
129
130         if (vpninfo->vpn_domain)
131                 setenv("CISCO_DEF_DOMAIN", vpninfo->vpn_domain, 1);
132         else unsetenv ("CISCO_DEF_DOMAIN");
133 }
134
135 static int script_config_tun(struct openconnect_info *vpninfo)
136 {
137         if (vpninfo->peer_addr->sa_family != AF_INET) {
138                 vpninfo->progress(vpninfo, PRG_ERR, "Script cannot handle anything but Legacy IP\n");
139                 return -EINVAL;
140         }
141
142         set_script_env(vpninfo);
143
144         system(vpninfo->vpnc_script);
145         return 0;
146 }
147
148
149 /* Set up a tuntap device. */
150 int setup_tun(struct openconnect_info *vpninfo)
151 {
152         struct ifreq ifr;
153         int tun_fd;
154         int pfd;
155
156         if (vpninfo->script_tun) {
157                 pid_t child;
158                 int fds[2];
159
160                 if (socketpair(AF_UNIX, SOCK_DGRAM, 0, fds)) {
161                         perror("socketpair");
162                         exit(1);
163                 }
164                 tun_fd = fds[0];
165                 child = fork();
166                 if (child < 0) {
167                         perror("fork");
168                         exit(1);
169                 } else if (!child) {
170                         close(tun_fd);
171                         setenv_int("VPNFD", fds[1]);
172                         execl("/bin/sh", "/bin/sh", "-c", vpninfo->vpnc_script, NULL);
173                         perror("execl");
174                         exit(1);
175                 }
176                 close(fds[1]);
177                 vpninfo->script_tun = child;
178                 vpninfo->ifname = "(script)";
179         } else {
180                 tun_fd = open("/dev/net/tun", O_RDWR);
181                 if (tun_fd < 0) {
182                         perror("open tun");
183                         exit(1);
184                 }
185                 memset(&ifr, 0, sizeof(ifr));
186                 ifr.ifr_flags = IFF_TUN | IFF_NO_PI;
187                 if (vpninfo->ifname)
188                         strncpy(ifr.ifr_name, vpninfo->ifname,
189                                 sizeof(ifr.ifr_name) - 1);
190                 if (ioctl(tun_fd, TUNSETIFF, (void *) &ifr) < 0) {
191                         perror("TUNSETIFF");
192                         exit(1);
193                 }
194                 if (!vpninfo->ifname)
195                         vpninfo->ifname = strdup(ifr.ifr_name);
196
197         fcntl(tun_fd, F_SETFD, FD_CLOEXEC);
198
199         if (vpninfo->vpnc_script) {
200                 script_config_tun(vpninfo);
201                 /* We have to set the MTU for ourselves, because the script doesn't */
202                 local_config_tun(vpninfo, 1);
203         } else
204                 local_config_tun(vpninfo, 0);
205         }
206
207         vpninfo->tun_fd = tun_fd;
208         pfd = vpn_add_pollfd(vpninfo, vpninfo->tun_fd, POLLIN);
209
210         fcntl(vpninfo->tun_fd, F_SETFL, fcntl(vpninfo->tun_fd, F_GETFL) | O_NONBLOCK);
211
212         return 0;
213 }
214
215 int tun_mainloop(struct openconnect_info *vpninfo, int *timeout)
216 {
217         char buf[2000];
218         int len;
219         int work_done = 0;
220
221         while ( (len = read(vpninfo->tun_fd, buf, sizeof(buf))) > 0) {
222                 queue_new_packet(&vpninfo->outgoing_queue, AF_INET, buf, len);
223                 work_done = 1;
224         }
225
226         /* The kernel returns -ENOMEM when the queue is full, so theoretically
227            we could handle that and retry... but it doesn't let us poll() for
228            the no-longer-full situation, so let's not bother. */
229         while (vpninfo->incoming_queue) {
230                 struct pkt *this = vpninfo->incoming_queue;
231                 vpninfo->incoming_queue = this->next;
232                 if (write(vpninfo->tun_fd, this->data, this->len) < 0 &&
233                     errno == ENOTCONN) {
234                         vpninfo->quit_reason = "Client connection terminated";
235                         return 1;
236                 }
237         }
238         /* Work is not done if we just got rid of packets off the queue */
239         return work_done;
240 }