Add MacOS support to tun.c
[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 <sys/socket.h>
26 #include <net/if.h>
27 #include <sys/ioctl.h>
28 #ifndef __APPLE__
29 #include <linux/if_tun.h>
30 #endif
31 #include <sys/types.h>
32 #include <sys/stat.h>
33 #include <fcntl.h>
34 #include <unistd.h>
35 #include <netinet/in.h>
36 #include <arpa/inet.h>
37 #include <errno.h>
38
39 #include "openconnect.h"
40
41 static int local_config_tun(struct openconnect_info *vpninfo, int mtu_only)
42 {
43         struct ifreq ifr;
44         int net_fd;
45
46         net_fd = socket(PF_INET, SOCK_DGRAM, 0);
47         if (net_fd < 0) {
48                 perror("open net");
49                 return -EINVAL;
50         }
51         memset(&ifr, 0, sizeof(ifr));
52         strncpy(ifr.ifr_name, vpninfo->ifname, sizeof(ifr.ifr_name) - 1);
53
54         if (!mtu_only) {
55                 struct sockaddr_in *addr = (struct sockaddr_in *) &ifr.ifr_addr;
56
57                 if (ioctl(net_fd, SIOCGIFFLAGS, &ifr) < 0)
58                         perror("SIOCGIFFLAGS");
59
60                 ifr.ifr_flags |= IFF_UP | IFF_POINTOPOINT; 
61                 if (ioctl(net_fd, SIOCSIFFLAGS, &ifr) < 0)
62                         perror("SIOCSIFFLAGS");
63
64                 addr->sin_family = AF_INET;
65                 addr->sin_addr.s_addr = inet_addr(vpninfo->vpn_addr);
66                 if (ioctl(net_fd, SIOCSIFADDR, &ifr) < 0)
67                         perror("SIOCSIFADDR");
68         }
69
70         ifr.ifr_mtu = vpninfo->mtu;
71         if (ioctl(net_fd, SIOCSIFMTU, &ifr) < 0)
72                 perror("SIOCSIFMTU");
73
74         close(net_fd);
75
76         return 0;
77 }
78
79 static int setenv_int(const char *opt, int value)
80 {
81         char buf[16];
82         sprintf(buf, "%d", value);
83         return setenv(opt, buf, 1);
84 }
85
86 static int appendenv(const char *opt, const char *new)
87 {
88         char buf[1024];
89         char *old = getenv(opt);
90
91         buf[1023] = 0;
92         if (old)
93                 snprintf(buf, 1023, "%s %s", old, new);
94         else
95                 snprintf(buf, 1023, "%s", new);
96
97         return setenv(opt, buf, 1);
98 }
99
100 static void set_script_env(struct openconnect_info *vpninfo)
101 {
102         struct sockaddr_in *sin = (void *)vpninfo->peer_addr;
103
104         setenv("VPNGATEWAY", inet_ntoa(sin->sin_addr), 1);
105         setenv("TUNDEV", vpninfo->ifname, 1);
106         setenv("reason", "connect", 1);
107         unsetenv("CISCO_BANNER");
108         unsetenv("CISCO_SPLIT_INC");
109
110         setenv_int("INTERNAL_IP4_MTU", vpninfo->mtu);
111
112         setenv("INTERNAL_IP4_ADDRESS", vpninfo->vpn_addr, 1);
113         setenv("INTERNAL_IP4_NETMASK", vpninfo->vpn_netmask, 1);
114         
115         if (vpninfo->vpn_dns[0])
116                 setenv("INTERNAL_IP4_DNS", vpninfo->vpn_dns[0], 1);
117         else
118                 unsetenv("INTERNAL_IP4_DNS");
119         if (vpninfo->vpn_dns[1])
120                 appendenv("INTERNAL_IP4_DNS", vpninfo->vpn_dns[1]);
121         if (vpninfo->vpn_dns[2])
122                 appendenv("INTERNAL_IP4_DNS", vpninfo->vpn_dns[2]);
123
124         if (vpninfo->vpn_nbns[0])
125                 setenv("INTERNAL_IP4_NBNS", vpninfo->vpn_nbns[0], 1);
126         else
127                 unsetenv("INTERNAL_IP4_NBNS");
128         if (vpninfo->vpn_nbns[1])
129                 appendenv("INTERNAL_IP4_NBNS", vpninfo->vpn_nbns[1]);
130         if (vpninfo->vpn_nbns[2])
131                 appendenv("INTERNAL_IP4_NBNS", vpninfo->vpn_nbns[2]);
132
133         if (vpninfo->vpn_domain)
134                 setenv("CISCO_DEF_DOMAIN", vpninfo->vpn_domain, 1);
135         else unsetenv ("CISCO_DEF_DOMAIN");
136 }
137
138 static int script_config_tun(struct openconnect_info *vpninfo)
139 {
140         if (vpninfo->peer_addr->sa_family != AF_INET) {
141                 vpninfo->progress(vpninfo, PRG_ERR, "Script cannot handle anything but Legacy IP\n");
142                 return -EINVAL;
143         }
144
145         set_script_env(vpninfo);
146
147         system(vpninfo->vpnc_script);
148         return 0;
149 }
150
151
152 /* Set up a tuntap device. */
153 int setup_tun(struct openconnect_info *vpninfo)
154 {
155         struct ifreq ifr;
156         int tun_fd;
157         int pfd;
158
159         if (vpninfo->script_tun) {
160                 pid_t child;
161                 int fds[2];
162
163                 if (socketpair(AF_UNIX, SOCK_DGRAM, 0, fds)) {
164                         perror("socketpair");
165                         exit(1);
166                 }
167                 tun_fd = fds[0];
168                 child = fork();
169                 if (child < 0) {
170                         perror("fork");
171                         exit(1);
172                 } else if (!child) {
173                         close(tun_fd);
174                         setenv_int("VPNFD", fds[1]);
175                         execl("/bin/sh", "/bin/sh", "-c", vpninfo->vpnc_script, NULL);
176                         perror("execl");
177                         exit(1);
178                 }
179                 close(fds[1]);
180                 vpninfo->script_tun = child;
181                 vpninfo->ifname = "(script)";
182         } else {
183 #ifdef __APPLE__
184                 static char tun_name[80];
185                 int i;
186                 for (i=0; i < 255; i++) {
187                         sprintf(tun_name, "/dev/tun%d", i);
188                         tun_fd = open(tun_name, O_RDWR);
189                         if (tun_fd >= 0)
190                                 break;
191                 }
192                 if (tun_fd < 0) {
193                         perror("open tun");
194                         exit(1);
195                 }
196                 vpninfo->ifname = tun_name + 5;
197 #else
198
199                 tun_fd = open("/dev/net/tun", O_RDWR);
200                 if (tun_fd < 0) {
201                         perror("open tun");
202                         exit(1);
203                 }
204                 memset(&ifr, 0, sizeof(ifr));
205                 ifr.ifr_flags = IFF_TUN | IFF_NO_PI;
206                 if (vpninfo->ifname)
207                         strncpy(ifr.ifr_name, vpninfo->ifname,
208                                 sizeof(ifr.ifr_name) - 1);
209                 if (ioctl(tun_fd, TUNSETIFF, (void *) &ifr) < 0) {
210                         perror("TUNSETIFF");
211                         exit(1);
212                 }
213                 if (!vpninfo->ifname)
214                         vpninfo->ifname = strdup(ifr.ifr_name);
215
216 #endif
217                 if (vpninfo->vpnc_script) {
218                         script_config_tun(vpninfo);
219                         /* We have to set the MTU for ourselves, because the script doesn't */
220                         local_config_tun(vpninfo, 1);
221                 } else 
222                         local_config_tun(vpninfo, 0);
223         }
224
225         fcntl(tun_fd, F_SETFD, FD_CLOEXEC);
226
227         vpninfo->tun_fd = tun_fd;
228         pfd = vpn_add_pollfd(vpninfo, vpninfo->tun_fd, POLLIN);
229
230         fcntl(vpninfo->tun_fd, F_SETFL, fcntl(vpninfo->tun_fd, F_GETFL) | O_NONBLOCK);
231
232         return 0;
233 }
234
235 int tun_mainloop(struct openconnect_info *vpninfo, int *timeout)
236 {
237         char buf[2000];
238         int len;
239         int work_done = 0;
240
241         while ( (len = read(vpninfo->tun_fd, buf, sizeof(buf))) > 0) {
242                 queue_new_packet(&vpninfo->outgoing_queue, AF_INET, buf, len);
243                 work_done = 1;
244         }
245
246         /* The kernel returns -ENOMEM when the queue is full, so theoretically
247            we could handle that and retry... but it doesn't let us poll() for
248            the no-longer-full situation, so let's not bother. */
249         while (vpninfo->incoming_queue) {
250                 struct pkt *this = vpninfo->incoming_queue;
251                 vpninfo->incoming_queue = this->next;
252                 if (write(vpninfo->tun_fd, this->data, this->len) < 0 &&
253                     errno == ENOTCONN) {
254                         vpninfo->quit_reason = "Client connection terminated";
255                         return 1;
256                 }
257         }
258         /* Work is not done if we just got rid of packets off the queue */
259         return work_done;
260 }