[svn-upgrade] Integrating new upstream version, iodine (0.5.0)
[debian/iodine.git] / src / tun.c
1 /*
2  * Copyright (c) 2006-2009 Bjorn Andersson <flex@kryo.se>, Erik Ekman <yarrick@kryo.se>
3  *
4  * Permission to use, copy, modify, and distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15  */
16
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <unistd.h>
20 #include <string.h>
21 #include <errno.h>
22 #include <stdint.h>
23 #include <sys/types.h>
24 #include <sys/stat.h>
25 #include <fcntl.h>
26 #include <err.h>
27 #include <arpa/inet.h>
28 #include <netinet/in.h>
29
30 #include "tun.h"
31
32 #define TUN_MAX_TRY 50
33
34 char if_name[50];
35
36 #ifdef LINUX
37
38 #include <sys/ioctl.h>
39 #include <net/if.h>
40 #include <linux/if_tun.h>
41
42 int 
43 open_tun(const char *tun_device) 
44 {
45         int i;
46         int tun_fd;
47         struct ifreq ifreq;
48         char *tunnel = "/dev/net/tun";
49
50         if ((tun_fd = open(tunnel, O_RDWR)) < 0) {
51                 warn("open_tun: %s: %s", tunnel, strerror(errno));
52                 return -1;
53         }
54
55         memset(&ifreq, 0, sizeof(ifreq));
56
57         ifreq.ifr_flags = IFF_TUN; 
58
59         if (tun_device != NULL) {
60                 strncpy(ifreq.ifr_name, tun_device, IFNAMSIZ);
61                 ifreq.ifr_name[IFNAMSIZ-1] = '\0';
62                 strncpy(if_name, tun_device, sizeof(if_name));
63                 if_name[sizeof(if_name)-1] = '\0';
64
65                 if (ioctl(tun_fd, TUNSETIFF, (void *) &ifreq) != -1) {
66                         printf("Opened %s\n", ifreq.ifr_name);
67                         return tun_fd;
68                 }
69
70                 if (errno != EBUSY) {
71                         warn("open_tun: ioctl[TUNSETIFF]: %s", strerror(errno));
72                         return -1;
73                 }
74         } else {
75                 for (i = 0; i < TUN_MAX_TRY; i++) {
76                         snprintf(ifreq.ifr_name, IFNAMSIZ, "dns%d", i);
77
78                         if (ioctl(tun_fd, TUNSETIFF, (void *) &ifreq) != -1) {
79                                 printf("Opened %s\n", ifreq.ifr_name);
80                                 snprintf(if_name, sizeof(if_name), "dns%d", i);
81                                 return tun_fd;
82                         }
83
84                         if (errno != EBUSY) {
85                                 warn("open_tun: ioctl[TUNSETIFF]: %s", strerror(errno));
86                                 return -1;
87                         }
88                 }
89
90                 warn("open_tun: Couldn't set interface name");
91         }
92         return -1;
93 }
94
95 #else /* BSD */
96
97 int 
98 open_tun(const char *tun_device) 
99 {
100         int i;
101         int tun_fd;
102         char tun_name[50];
103
104         if (tun_device != NULL) {
105                 snprintf(tun_name, sizeof(tun_name), "/dev/%s", tun_device);
106                 strncpy(if_name, tun_device, sizeof(if_name));
107                 if_name[sizeof(if_name)-1] = '\0';
108
109                 if ((tun_fd = open(tun_name, O_RDWR)) < 0) {
110                         warn("open_tun: %s: %s", tun_name, strerror(errno));
111                         return -1;
112                 }
113
114                 printf("Opened %s\n", tun_name);
115                 return tun_fd;
116         } else {
117                 for (i = 0; i < TUN_MAX_TRY; i++) {
118                         snprintf(tun_name, sizeof(tun_name), "/dev/tun%d", i);
119
120                         if ((tun_fd = open(tun_name, O_RDWR)) >= 0) {
121                                 printf("Opened %s\n", tun_name);
122                                 snprintf(if_name, sizeof(if_name), "tun%d", i);
123                                 return tun_fd;
124                         }
125
126                         if (errno == ENOENT)
127                                 break;
128                 }
129
130                 warn("open_tun: Failed to open tunneling device");
131         }
132
133         return -1;
134 }
135
136 #endif /* !LINUX */
137
138 void 
139 close_tun(int tun_fd) 
140 {
141         if (tun_fd >= 0)
142                 close(tun_fd);
143 }
144
145 int 
146 write_tun(int tun_fd, char *data, size_t len) 
147 {
148 #if defined (FREEBSD) || defined (DARWIN) || defined(NETBSD)
149         data += 4;
150         len -= 4;
151 #else /* !FREEBSD/DARWIN */
152 #ifdef LINUX
153         data[0] = 0x00;
154         data[1] = 0x00;
155         data[2] = 0x08;
156         data[3] = 0x00;
157 #else /* OPENBSD */
158         data[0] = 0x00;
159         data[1] = 0x00;
160         data[2] = 0x00;
161         data[3] = 0x02;
162 #endif /* !LINUX */
163 #endif /* FREEBSD */
164
165         if (write(tun_fd, data, len) != len) {
166                 warn("write_tun");
167                 return 1;
168         }
169         return 0;
170 }
171
172 ssize_t
173 read_tun(int tun_fd, char *buf, size_t len) 
174 {
175 #if defined (FREEBSD) || defined (DARWIN) || defined(NETBSD)
176         /* FreeBSD/Darwin/NetBSD has no header */
177         return read(tun_fd, buf + 4, len - 4) + 4;
178 #else /* !FREEBSD */
179         return read(tun_fd, buf, len);
180 #endif /* !FREEBSD */
181 }
182
183 int
184 tun_setip(const char *ip, int netbits)
185 {
186         char cmdline[512];
187         int netmask;
188         struct in_addr net;
189         int i;
190
191 #ifndef LINUX
192         int r;
193 #endif
194         netmask = 0;
195         for (i = 0; i < netbits; i++) {
196                 netmask = (netmask << 1) | 1;
197         }
198         netmask <<= (32 - netbits);
199         net.s_addr = htonl(netmask);
200
201         if (inet_addr(ip) != INADDR_NONE) {
202                 snprintf(cmdline, sizeof(cmdline), 
203                                 "/sbin/ifconfig %s %s %s netmask %s",
204                                 if_name,
205                                 ip,
206                                 ip,
207                                 inet_ntoa(net));
208                 
209                 printf("Setting IP of %s to %s\n", if_name, ip);
210 #ifndef LINUX
211                 r = system(cmdline);
212                 if(r != 0) {
213                         return r;
214                 } else {
215                         snprintf(cmdline, sizeof(cmdline),
216                                         "/sbin/route add %s/%d %s",
217                                         ip, netbits, ip);
218                 }
219                 printf("Adding route %s/%d to %s\n", ip, netbits, ip);
220 #endif
221                 return system(cmdline);
222         } else {
223                 printf("Invalid IP: %s!\n", ip);
224         }
225
226         return 1;
227 }
228
229 int 
230 tun_setmtu(const unsigned mtu)
231 {
232         char cmdline[512];
233
234         if (mtu > 200 && mtu < 1500) {
235                 snprintf(cmdline, sizeof(cmdline), 
236                                 "/sbin/ifconfig %s mtu %u",
237                                 if_name,
238                                 mtu);
239                 
240                 printf("Setting MTU of %s to %u\n", if_name, mtu);
241                 return system(cmdline);
242         } else {
243                 warn("MTU out of range: %u\n", mtu);
244         }
245
246         return 1;
247 }
248