Add tun/tap support for Solaris
[platform/upstream/openconnect.git] / tun.c
1 /*
2  * OpenConnect (SSL + DTLS) VPN client
3  *
4  * Copyright © 2008 Intel Corporation.
5  *
6  * Author: David Woodhouse <dwmw2@infradead.org>
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public License
10  * version 2.1, as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to:
19  *
20  *   Free Software Foundation, Inc.
21  *   51 Franklin Street, Fifth Floor,
22  *   Boston, MA 02110-1301 USA
23  */
24
25 #include <sys/types.h>
26 #include <sys/stat.h>
27 #include <sys/socket.h>
28 #include <sys/ioctl.h>
29 #include <string.h>
30 #include <signal.h>
31 #ifdef __linux__
32 #include <linux/if_tun.h>
33 #elif defined(__sun__)
34 #include <net/if_tun.h>
35 #include <stropts.h>
36 #include <sys/sockio.h>
37 #endif
38 #include <fcntl.h>
39 #include <unistd.h>
40 #include <netinet/in_systm.h>
41 #include <netinet/in.h>
42 #include <netinet/ip.h>
43 #include <net/if.h>
44 #include <arpa/inet.h>
45 #include <errno.h>
46
47 #include "openconnect.h"
48
49 #ifdef __OpenBSD__
50 #define TUN_HAS_AF_PREFIX 1
51 #endif
52
53 #ifdef __sun__
54 static int local_config_tun(struct openconnect_info *vpninfo, int mtu_only)
55 {
56         if (!mtu_only)
57                 vpninfo->progress(vpninfo, PRG_ERR,
58                                   "No vpnc-script configured. Need Solaris IP-setting code\n");
59         return 0;
60 }
61 #else
62 static int local_config_tun(struct openconnect_info *vpninfo, int mtu_only)
63 {
64         struct ifreq ifr;
65         int net_fd;
66
67         net_fd = socket(PF_INET, SOCK_DGRAM, 0);
68         if (net_fd < 0) {
69                 perror("open net");
70                 return -EINVAL;
71         }
72         memset(&ifr, 0, sizeof(ifr));
73         strncpy(ifr.ifr_name, vpninfo->ifname, sizeof(ifr.ifr_name) - 1);
74
75         if (!mtu_only) {
76                 struct sockaddr_in addr;
77
78                 if (ioctl(net_fd, SIOCGIFFLAGS, &ifr) < 0)
79                         perror("SIOCGIFFLAGS");
80
81                 ifr.ifr_flags |= IFF_UP | IFF_POINTOPOINT;
82                 if (ioctl(net_fd, SIOCSIFFLAGS, &ifr) < 0)
83                         perror("SIOCSIFFLAGS");
84
85                 addr.sin_family = AF_INET;
86                 addr.sin_addr.s_addr = inet_addr(vpninfo->vpn_addr);
87                 memcpy(&ifr.ifr_addr, &addr, sizeof(addr));
88                 if (ioctl(net_fd, SIOCSIFADDR, &ifr) < 0)
89                         perror("SIOCSIFADDR");
90         }
91
92         ifr.ifr_mtu = vpninfo->mtu;
93         if (ioctl(net_fd, SIOCSIFMTU, &ifr) < 0)
94                 perror("SIOCSIFMTU");
95
96         close(net_fd);
97
98         return 0;
99 }
100 #endif
101
102 static int setenv_int(const char *opt, int value)
103 {
104         char buf[16];
105         sprintf(buf, "%d", value);
106         return setenv(opt, buf, 1);
107 }
108
109 static int process_split_xxclude(struct openconnect_info *vpninfo,
110                                  char *in_ex, char *route, int *nr_incs)
111 {
112         struct in_addr addr;
113         int masklen;
114         char envname[80];
115         char *slash;
116
117         slash = strchr(route, '/');
118         if (!slash) {
119         badinc:
120                 vpninfo->progress(vpninfo, PRG_ERR,
121                                   "Discard bad split %sclude: \"%s\"\n",
122                                   in_ex, route);
123                 return -EINVAL;
124         }
125
126         *slash = 0;
127         if (!inet_aton(route, &addr)) {
128                 *slash = '/';
129                 goto badinc;
130         }
131
132         envname[79] = 0;
133         snprintf(envname, 79, "CISCO_SPLIT_%sC_%d_ADDR", in_ex, *nr_incs);
134         setenv(envname, route, 1);
135
136         /* Put it back how we found it */
137         *slash = '/';
138
139         if (!inet_aton(slash+1, &addr))
140                 goto badinc;
141
142         snprintf(envname, 79, "CISCO_SPLIT_%sC_%d_MASK", in_ex, *nr_incs);
143         setenv(envname, slash+1, 1);
144
145         for (masklen = 0; masklen < 32; masklen++) {
146                 if (ntohl(addr.s_addr) >= (0xffffffff << masklen))
147                         break;
148         }
149         masklen = 32 - masklen;
150
151         snprintf(envname, 79, "CISCO_SPLIT_%sC_%d_MASKLEN", in_ex, *nr_incs);
152         setenv_int(envname, masklen);
153
154         (*nr_incs)++;
155         return 0;
156 }
157
158 static int appendenv(const char *opt, const char *new)
159 {
160         char buf[1024];
161         char *old = getenv(opt);
162
163         buf[1023] = 0;
164         if (old)
165                 snprintf(buf, 1023, "%s %s", old, new);
166         else
167                 snprintf(buf, 1023, "%s", new);
168
169         return setenv(opt, buf, 1);
170 }
171
172 static void setenv_cstp_opts(struct openconnect_info *vpninfo)
173 {
174         char *env_buf;
175         int buflen = 0;
176         int bufofs = 0;
177         struct vpn_option *opt;
178
179         for (opt = vpninfo->cstp_options; opt; opt = opt->next)
180                 buflen += 2 + strlen(opt->option) + strlen(opt->value);
181
182         env_buf = malloc(buflen + 1);
183         if (!env_buf)
184                 return;
185
186         env_buf[buflen] = 0;
187
188         for (opt = vpninfo->cstp_options; opt; opt = opt->next)
189                 bufofs += snprintf(env_buf + bufofs, buflen - bufofs,
190                                    "%s=%s\n", opt->option, opt->value);
191
192         setenv("CISCO_CSTP_OPTIONS", env_buf, 1);
193         free(env_buf);
194 }
195
196 static void set_script_env(struct openconnect_info *vpninfo)
197 {
198         struct sockaddr_in *sin = (void *)vpninfo->peer_addr;
199
200         setenv("VPNGATEWAY", inet_ntoa(sin->sin_addr), 1);
201         setenv("TUNDEV", vpninfo->ifname, 1);
202         setenv("reason", "connect", 1);
203         unsetenv("CISCO_BANNER");
204         unsetenv("CISCO_SPLIT_INC");
205         unsetenv("CISCO_SPLIT_EXC");
206
207         setenv_int("INTERNAL_IP4_MTU", vpninfo->mtu);
208
209         if (vpninfo->vpn_addr) {
210                 setenv("INTERNAL_IP4_ADDRESS", vpninfo->vpn_addr, 1);
211                 setenv("INTERNAL_IP4_NETMASK", vpninfo->vpn_netmask, 1);
212         }
213         if (vpninfo->vpn_addr6) {
214                 setenv("INTERNAL_IP6_ADDRESS", vpninfo->vpn_addr6, 1);
215                 setenv("INTERNAL_IP6_NETMASK", vpninfo->vpn_netmask6, 1);
216         }
217
218         if (vpninfo->vpn_dns[0])
219                 setenv("INTERNAL_IP4_DNS", vpninfo->vpn_dns[0], 1);
220         else
221                 unsetenv("INTERNAL_IP4_DNS");
222         if (vpninfo->vpn_dns[1])
223                 appendenv("INTERNAL_IP4_DNS", vpninfo->vpn_dns[1]);
224         if (vpninfo->vpn_dns[2])
225                 appendenv("INTERNAL_IP4_DNS", vpninfo->vpn_dns[2]);
226
227         if (vpninfo->vpn_nbns[0])
228                 setenv("INTERNAL_IP4_NBNS", vpninfo->vpn_nbns[0], 1);
229         else
230                 unsetenv("INTERNAL_IP4_NBNS");
231         if (vpninfo->vpn_nbns[1])
232                 appendenv("INTERNAL_IP4_NBNS", vpninfo->vpn_nbns[1]);
233         if (vpninfo->vpn_nbns[2])
234                 appendenv("INTERNAL_IP4_NBNS", vpninfo->vpn_nbns[2]);
235
236         if (vpninfo->vpn_domain)
237                 setenv("CISCO_DEF_DOMAIN", vpninfo->vpn_domain, 1);
238         else unsetenv ("CISCO_DEF_DOMAIN");
239
240         if (vpninfo->vpn_proxy_pac)
241                 setenv("CISCO_PROXY_PAC", vpninfo->vpn_proxy_pac, 1);
242
243         if (vpninfo->split_includes) {
244                 struct split_include *this = vpninfo->split_includes;
245                 int nr_split_includes = 0;
246
247                 while (this) {
248                         process_split_xxclude(vpninfo, "IN", this->route,
249                                               &nr_split_includes);
250                         this = this->next;
251                 }
252                 setenv_int("CISCO_SPLIT_INC", nr_split_includes);
253         }
254         if (vpninfo->split_excludes) {
255                 struct split_include *this = vpninfo->split_excludes;
256                 int nr_split_excludes = 0;
257
258                 while (this) {
259                         process_split_xxclude(vpninfo, "EX", this->route,
260                                               &nr_split_excludes);
261                         this = this->next;
262                 }
263                 setenv_int("CISCO_SPLIT_EXC", nr_split_excludes);
264         }
265         setenv_cstp_opts(vpninfo);
266 }
267
268 static int script_config_tun(struct openconnect_info *vpninfo)
269 {
270         if (vpninfo->peer_addr->sa_family != AF_INET || !vpninfo->vpn_addr) {
271                 vpninfo->progress(vpninfo, PRG_ERR,
272                                   "Script can only handle Legacy IP\n");
273                 return -EINVAL;
274         }
275
276         set_script_env(vpninfo);
277
278         system(vpninfo->vpnc_script);
279         return 0;
280 }
281
282
283 /* Set up a tuntap device. */
284 int setup_tun(struct openconnect_info *vpninfo)
285 {
286         int tun_fd;
287
288         if (vpninfo->script_tun) {
289                 pid_t child;
290                 int fds[2];
291
292                 if (socketpair(AF_UNIX, SOCK_DGRAM, 0, fds)) {
293                         perror("socketpair");
294                         exit(1);
295                 }
296                 tun_fd = fds[0];
297                 child = fork();
298                 if (child < 0) {
299                         perror("fork");
300                         exit(1);
301                 } else if (!child) {
302                         close(tun_fd);
303                         setenv_int("VPNFD", fds[1]);
304                         execl("/bin/sh", "/bin/sh", "-c", vpninfo->vpnc_script, NULL);
305                         perror("execl");
306                         exit(1);
307                 }
308                 close(fds[1]);
309                 vpninfo->script_tun = child;
310                 vpninfo->ifname = "(script)";
311         } else {
312 #ifdef IFF_TUN /* Linux */
313                 struct ifreq ifr;
314
315                 tun_fd = open("/dev/net/tun", O_RDWR);
316                 if (tun_fd < 0) {
317                         vpninfo->progress(vpninfo, PRG_ERR,
318                                           "Failed to open tun device: %s\n",
319                                           strerror(errno));
320                         exit(1);
321                 }
322                 memset(&ifr, 0, sizeof(ifr));
323                 ifr.ifr_flags = IFF_TUN | IFF_NO_PI;
324                 if (vpninfo->ifname)
325                         strncpy(ifr.ifr_name, vpninfo->ifname,
326                                 sizeof(ifr.ifr_name) - 1);
327                 if (ioctl(tun_fd, TUNSETIFF, (void *) &ifr) < 0) {
328                         vpninfo->progress(vpninfo, PRG_ERR,
329                                           "TUNSETIFF failed: %s\n",
330                                           strerror(errno));
331                         exit(1);
332                 }
333                 if (!vpninfo->ifname)
334                         vpninfo->ifname = strdup(ifr.ifr_name);
335 #elif defined (__sun__)
336                 static char tun_name[80];
337                 int tun2_fd, ip_fd = open("/dev/ip", O_RDWR);
338                 int unit_nr, mux_id;
339                 struct ifreq ifr;
340
341                 if (ip_fd < 0) {
342                         perror("open /dev/ip");
343                         return -EIO;
344                 }
345
346                 tun_fd = open("/dev/tun", O_RDWR);
347                 if (tun_fd < 0) {
348                         perror("open /dev/tun");
349                         close(ip_fd);
350                         return -EIO;
351                 }
352
353                 unit_nr = ioctl(tun_fd, TUNNEWPPA, -1);
354                 if (unit_nr < 0) {
355                         perror("Failed to create new tun");
356                         close(tun_fd);
357                         close(ip_fd);
358                         return -EIO;
359                 }
360                 
361                 tun2_fd = open("/dev/tun", O_RDWR);
362                 if (tun2_fd < 0) {
363                         perror("open /dev/tun again");
364                         close(tun_fd);
365                         close(ip_fd);
366                         return -EIO;
367                 }
368                 if (ioctl(tun2_fd, I_PUSH, "ip") < 0) {
369                         perror("Can't push IP");
370                         close(tun2_fd);
371                         close(tun_fd);
372                         close(ip_fd);
373                         return -EIO;
374                 }
375                 if (ioctl(tun2_fd, IF_UNITSEL, &unit_nr) < 0) {
376                         perror("Can't select unit");
377                         close(tun2_fd);
378                         close(tun_fd);
379                         close(ip_fd);
380                         return -EIO;
381                 }
382                 mux_id = ioctl(ip_fd, I_PLINK, tun2_fd);
383                 if (mux_id < 0) {
384                         perror("Can't link tun to IP");
385                         close(tun2_fd);
386                         close(tun_fd);
387                         close(ip_fd);
388                         return -EIO;
389                 }
390                 close(tun2_fd);
391
392                 sprintf(tun_name, "tun%d", unit_nr);
393                 vpninfo->ifname = tun_name;
394
395                 memset(&ifr, 0, sizeof(ifr));
396                 strcpy(ifr.ifr_name, tun_name);
397                 ifr.ifr_ip_muxid = mux_id;
398
399                 if (ioctl(ip_fd, SIOCSIFMUXID, &ifr) < 0) {
400                         perror("Set mux id");
401                         close(tun_fd);
402                         ioctl(ip_fd, I_PUNLINK, mux_id);
403                         close(ip_fd);
404                         return -EIO;
405                 }
406                 /* Solaris tunctl needs this in order to tear it down */
407                 vpninfo->progress(vpninfo, PRG_DEBUG, "mux id is %d\n", mux_id);
408                 vpninfo->tun_muxid = mux_id;
409                 vpninfo->ip_fd = ip_fd;
410
411 #else /* BSD et al have /dev/tun$x devices */
412                 static char tun_name[80];
413                 int i;
414                 for (i = 0; i < 255; i++) {
415                         sprintf(tun_name, "/dev/tun%d", i);
416                         tun_fd = open(tun_name, O_RDWR);
417                         if (tun_fd >= 0)
418                                 break;
419                 }
420                 if (tun_fd < 0) {
421                         perror("open tun");
422                         exit(1);
423                 }
424                 vpninfo->ifname = tun_name + 5;
425 #endif
426                 if (vpninfo->vpnc_script) {
427                         script_config_tun(vpninfo);
428                         /* We have to set the MTU for ourselves, because the script doesn't */
429                         local_config_tun(vpninfo, 1);
430                 } else
431                         local_config_tun(vpninfo, 0);
432         }
433
434         fcntl(tun_fd, F_SETFD, FD_CLOEXEC);
435
436         vpninfo->tun_fd = tun_fd;
437
438         if (vpninfo->select_nfds <= tun_fd)
439                 vpninfo->select_nfds = tun_fd + 1;
440
441         FD_SET(tun_fd, &vpninfo->select_rfds);
442
443         fcntl(vpninfo->tun_fd, F_SETFL, fcntl(vpninfo->tun_fd, F_GETFL) | O_NONBLOCK);
444
445         return 0;
446 }
447
448 int tun_mainloop(struct openconnect_info *vpninfo, int *timeout)
449 {
450         unsigned char buf[2000];
451         int len;
452         int work_done = 0;
453
454         if (FD_ISSET(vpninfo->tun_fd, &vpninfo->select_rfds)) {
455                 while ((len = read(vpninfo->tun_fd, buf, sizeof(buf))) > 0) {
456                         unsigned char *pkt = buf;
457 #ifdef TUN_HAS_AF_PREFIX
458                         pkt += 4;
459                         len -= 4;
460 #endif
461                         if (queue_new_packet(&vpninfo->outgoing_queue, pkt,
462                                              len))
463                                 break;
464
465                         work_done = 1;
466                         vpninfo->outgoing_qlen++;
467                         if (vpninfo->outgoing_qlen == vpninfo->max_qlen) {
468                                 FD_CLR(vpninfo->tun_fd, &vpninfo->select_rfds);
469                                 break;
470                         }
471                 }
472         } else if (vpninfo->outgoing_qlen < vpninfo->max_qlen) {
473                 FD_SET(vpninfo->tun_fd, &vpninfo->select_rfds);
474         }
475
476         /* The kernel returns -ENOMEM when the queue is full, so theoretically
477            we could handle that and retry... but it doesn't let us poll() for
478            the no-longer-full situation, so let's not bother. */
479         while (vpninfo->incoming_queue) {
480                 struct pkt *this = vpninfo->incoming_queue;
481                 unsigned char *data = this->data;
482                 int len = this->len;
483
484 #ifdef TUN_HAS_AF_PREFIX
485                 struct ip *iph = (void *)data;
486                 int type;
487
488                 if (iph->ip_v == 6)
489                         type = AF_INET6;
490                 else if (iph->ip_v == 4)
491                         type = AF_INET;
492                 else {
493                         static int complained = 0;
494                         if (!complained) {
495                                 complained = 1;
496                                 vpninfo->progress(vpninfo, PRG_ERR,
497                                                   "Unknown packet (len %d) received: %02x %02x %02x %02x...\n",
498                                                   len, data[0], data[1], data[2], data[3]);
499                         }
500                         free(this);
501                         continue;
502                 }
503                 data -= 4;
504                 len += 4;
505                 *(int *)data = htonl(type);
506 #endif
507                 vpninfo->incoming_queue = this->next;
508
509                 if (write(vpninfo->tun_fd, data, len) < 0 &&
510                     errno == ENOTCONN) {
511                         vpninfo->quit_reason = "Client connection terminated";
512                         return 1;
513                 }
514                 free(this);
515         }
516         /* Work is not done if we just got rid of packets off the queue */
517         return work_done;
518 }
519
520 void shutdown_tun(struct openconnect_info *vpninfo)
521 {       
522         if (vpninfo->script_tun) {
523                 kill(vpninfo->script_tun, SIGHUP);
524         } else {
525                 if (vpninfo->vpnc_script) {
526                         setenv("TUNDEV", vpninfo->ifname, 1);
527                         setenv("reason", "disconnect", 1);
528                         system(vpninfo->vpnc_script);
529                 }
530 #ifdef __sun__
531                 if (ioctl(vpninfo->ip_fd, I_PUNLINK, vpninfo->tun_muxid) < 0)
532                         perror("ioctl(I_PUNLINK)");
533
534                 close(vpninfo->ip_fd);
535                 vpninfo->ip_fd = -1;
536 #endif
537         }
538
539         close(vpninfo->tun_fd);
540         vpninfo->tun_fd = -1;
541 }