move environment setting to separate function
[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         tun_fd = open("/dev/net/tun", O_RDWR);
157         if (tun_fd < 0) {
158                 perror("open tun");
159                 exit(1);
160         }
161         memset(&ifr, 0, sizeof(ifr));
162         ifr.ifr_flags = IFF_TUN | IFF_NO_PI;
163         if (vpninfo->ifname)
164                 strncpy(ifr.ifr_name, vpninfo->ifname,
165                         sizeof(ifr.ifr_name) - 1);
166         if (ioctl(tun_fd, TUNSETIFF, (void *) &ifr) < 0) {
167                 perror("TUNSETIFF");
168                 exit(1);
169         }
170         if (!vpninfo->ifname)
171                 vpninfo->ifname = strdup(ifr.ifr_name);
172
173         fcntl(tun_fd, F_SETFD, FD_CLOEXEC);
174
175         if (vpninfo->vpnc_script) {
176                 script_config_tun(vpninfo);
177                 /* We have to set the MTU for ourselves, because the script doesn't */
178                 local_config_tun(vpninfo, 1);
179         } else
180                 local_config_tun(vpninfo, 0);
181
182         /* Better still, use lwip and just provide a SOCKS server rather than
183            telling the kernel about it at all */
184         vpninfo->tun_fd = tun_fd;
185         pfd = vpn_add_pollfd(vpninfo, vpninfo->tun_fd, POLLIN);
186
187         fcntl(vpninfo->tun_fd, F_SETFL, fcntl(vpninfo->tun_fd, F_GETFL) | O_NONBLOCK);
188
189         return 0;
190 }
191
192 int tun_mainloop(struct openconnect_info *vpninfo, int *timeout)
193 {
194         char buf[2000];
195         int len;
196         int work_done = 0;
197
198         while ( (len = read(vpninfo->tun_fd, buf, sizeof(buf))) > 0) {
199                 queue_new_packet(&vpninfo->outgoing_queue, AF_INET, buf, len);
200                 work_done = 1;
201         }
202
203         /* The kernel returns -ENOMEM when the queue is full, so theoretically
204            we could handle that and retry... but it doesn't let us poll() for
205            the no-longer-full situation, so let's not bother. */
206         while (vpninfo->incoming_queue) {
207                 struct pkt *this = vpninfo->incoming_queue;
208                 vpninfo->incoming_queue = this->next;
209                 write(vpninfo->tun_fd, this->data, this->len);
210         }
211         /* Work is not done if we just got rid of packets off the queue */
212         return work_done;
213 }