Change name of MTU environment variable
[platform/upstream/openconnect.git] / tun.c
1 /*
2  * Open AnyConnect (SSL + DTLS) client
3  *
4  * © 2008 David Woodhouse <dwmw2@infradead.org>
5  *
6  * Permission to use, copy, modify, and/or distribute this software
7  * for any purpose with or without fee is hereby granted, provided
8  * that the above copyright notice and this permission notice appear
9  * in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
12  * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
13  * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
14  * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
15  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
16  * OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
17  * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
18  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19  */
20
21 #include <string.h>
22 #include <net/if.h>
23 #include <sys/ioctl.h>
24 #include <linux/if_tun.h>
25 #include <sys/types.h>
26 #include <sys/stat.h>
27 #include <fcntl.h>
28 #include <unistd.h>
29 #include <netinet/in.h>
30 #include <arpa/inet.h>
31 #include <errno.h>
32
33 #include "anyconnect.h"
34
35 static int local_config_tun(struct anyconnect_info *vpninfo, int mtu_only)
36 {
37         struct ifreq ifr;
38         int net_fd;
39
40         net_fd = socket(PF_INET, SOCK_DGRAM, 0);
41         if (net_fd < 0) {
42                 perror("open net");
43                 return -EINVAL;
44         }
45         memset(&ifr, 0, sizeof(ifr));
46         strncpy(ifr.ifr_name, vpninfo->ifname, sizeof(ifr.ifr_name) - 1);
47
48         if (!mtu_only) {
49                 struct sockaddr_in *addr = (struct sockaddr_in *) &ifr.ifr_addr;
50
51                 if (ioctl(net_fd, SIOCGIFFLAGS, &ifr) < 0)
52                         perror("SIOCGIFFLAGS");
53
54                 ifr.ifr_flags |= IFF_UP;
55                 if (ioctl(net_fd, SIOCSIFFLAGS, &ifr) < 0)
56                         perror("SIOCSIFFLAGS");
57
58                 addr->sin_family = AF_INET;
59                 addr->sin_addr.s_addr = inet_addr(vpninfo->vpn_addr);
60                 if (ioctl(net_fd, SIOCSIFADDR, &ifr) < 0)
61                         perror("SIOCSIFADDR");
62
63                 addr->sin_addr.s_addr = inet_addr(vpninfo->vpn_netmask);
64                 if (ioctl(net_fd, SIOCSIFNETMASK, &ifr) < 0)
65                         perror("SIOCSIFNETMASK");
66         }
67
68         ifr.ifr_mtu = vpninfo->mtu;
69         if (ioctl(net_fd, SIOCSIFMTU, &ifr) < 0)
70                 perror("SIOCSIFMTU");
71
72         close(net_fd);
73
74         return 0;
75 }
76
77 static int setenv_int(const char *opt, int value)
78 {
79         char buf[16];
80         sprintf(buf, "%d", value);
81         return setenv(opt, buf, 1);
82 }
83
84 static int appendenv(const char *opt, const char *new)
85 {
86         char buf[1024];
87         char *old = getenv(opt);
88
89         buf[1023] = 0;
90         if (old)
91                 snprintf(buf, 1023, "%s %s", old, new);
92         else
93                 snprintf(buf, 1023, "%s", new);
94
95         return setenv(opt, buf, 1);
96 }
97
98 static int script_config_tun(struct anyconnect_info *vpninfo)
99 {
100         struct sockaddr_in *sin = (void *)vpninfo->peer_addr;
101
102         if (vpninfo->peer_addr->sa_family != AF_INET) {
103                 fprintf(stderr, "Script cannot handle anything but Legacy IP\n");
104                 return -EINVAL;
105         }
106
107         setenv("VPNGATEWAY", inet_ntoa(sin->sin_addr), 1);
108         setenv("TUNDEV", vpninfo->ifname, 1);
109         setenv("reason", "connect", 1);
110         unsetenv("CISCO_BANNER");
111         unsetenv("CISCO_SPLIT_INC");
112
113         setenv_int("INTERNAL_IP4_MTU", vpninfo->mtu);
114
115         setenv("INTERNAL_IP4_ADDRESS", vpninfo->vpn_addr, 1);
116         setenv("INTERNAL_IP4_NETMASK", vpninfo->vpn_netmask, 1);
117         
118         if (vpninfo->vpn_dns[0])
119                 setenv("INTERNAL_IP4_DNS", vpninfo->vpn_dns[0], 1);
120         else
121                 unsetenv("INTERNAL_IP4_DNS");
122         if (vpninfo->vpn_dns[1])
123                 appendenv("INTERNAL_IP4_DNS", vpninfo->vpn_dns[1]);
124         if (vpninfo->vpn_dns[2])
125                 appendenv("INTERNAL_IP4_DNS", vpninfo->vpn_dns[2]);
126
127         if (vpninfo->vpn_nbns[0])
128                 setenv("INTERNAL_IP4_NBNS", vpninfo->vpn_nbns[0], 1);
129         else
130                 unsetenv("INTERNAL_IP4_NBNS");
131         if (vpninfo->vpn_nbns[1])
132                 appendenv("INTERNAL_IP4_NBNS", vpninfo->vpn_nbns[1]);
133         if (vpninfo->vpn_nbns[2])
134                 appendenv("INTERNAL_IP4_NBNS", vpninfo->vpn_nbns[2]);
135
136         if (vpninfo->vpn_domain)
137                 setenv("CISCO_DEF_DOMAIN", vpninfo->vpn_domain, 1);
138         else unsetenv ("CISCO_DEF_DOMAIN");
139
140         system(vpninfo->vpnc_script);
141         return 0;
142 }
143
144
145 /* Set up a tuntap device. */
146 int setup_tun(struct anyconnect_info *vpninfo)
147 {
148         struct ifreq ifr;
149         int tun_fd;
150         int pfd;
151
152         tun_fd = open("/dev/net/tun", O_RDWR);
153         if (tun_fd < 0) {
154                 perror("open tun");
155                 exit(1);
156         }
157         memset(&ifr, 0, sizeof(ifr));
158         ifr.ifr_flags = IFF_TUN | IFF_NO_PI;
159         strncpy(ifr.ifr_name, vpninfo->ifname, sizeof(ifr.ifr_name) - 1);
160         if (ioctl(tun_fd, TUNSETIFF, (void *) &ifr) < 0) {
161                 perror("TUNSETIFF");
162                 exit(1);
163         }
164
165         fcntl(tun_fd, F_SETFD, FD_CLOEXEC);
166
167         if (vpninfo->vpnc_script) {
168                 script_config_tun(vpninfo);
169                 /* We have to set the MTU for ourselves, because the script doesn't */
170                 local_config_tun(vpninfo, 1);
171         } else
172                 local_config_tun(vpninfo, 0);
173
174         /* Better still, use lwip and just provide a SOCKS server rather than
175            telling the kernel about it at all */
176         vpninfo->tun_fd = tun_fd;
177         pfd = vpn_add_pollfd(vpninfo, vpninfo->tun_fd, POLLIN);
178
179         fcntl(vpninfo->tun_fd, F_SETFL, fcntl(vpninfo->tun_fd, F_GETFL) | O_NONBLOCK);
180
181         return 0;
182 }
183
184 int tun_mainloop(struct anyconnect_info *vpninfo, int *timeout)
185 {
186         char buf[2000];
187         int len;
188         int work_done = 0;
189
190         while ( (len = read(vpninfo->tun_fd, buf, sizeof(buf))) > 0) {
191                 queue_new_packet(&vpninfo->outgoing_queue, AF_INET, buf, len);
192                 work_done = 1;
193         }
194
195         /* The kernel returns -ENOMEM when the queue is full, so theoretically
196            we could handle that and retry... but it doesn't let us poll() for
197            the no-longer-full situation, so let's not bother. */
198         while (vpninfo->incoming_queue) {
199                 struct pkt *this = vpninfo->incoming_queue;
200                 vpninfo->incoming_queue = this->next;
201                 write(vpninfo->tun_fd, this->data, this->len);
202         }
203         /* Work is not done if we just got rid of packets off the queue */
204         return work_done;
205 }