[svn-upgrade] Integrating new upstream version, iodine (0.4.0)
[debian/iodine.git] / src / tun.c
1 /*
2  * Copyright (c) 2006-2007 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                 strncpy(if_name, tun_device, sizeof(if_name));
62
63                 if (ioctl(tun_fd, TUNSETIFF, (void *) &ifreq) != -1) {
64                         printf("Opened %s\n", ifreq.ifr_name);
65                         return tun_fd;
66                 }
67
68                 if (errno != EBUSY) {
69                         warn("open_tun: ioctl[TUNSETIFF]: %s", strerror(errno));
70                         return -1;
71                 }
72         } else {
73                 for (i = 0; i < TUN_MAX_TRY; i++) {
74                         snprintf(ifreq.ifr_name, IFNAMSIZ, "dns%d", i);
75
76                         if (ioctl(tun_fd, TUNSETIFF, (void *) &ifreq) != -1) {
77                                 printf("Opened %s\n", ifreq.ifr_name);
78                                 snprintf(if_name, sizeof(if_name), "dns%d", i);
79                                 return tun_fd;
80                         }
81
82                         if (errno != EBUSY) {
83                                 warn("open_tun: ioctl[TUNSETIFF]: %s", strerror(errno));
84                                 return -1;
85                         }
86                 }
87
88                 warn("open_tun: Couldn't set interface name");
89         }
90         return -1;
91 }
92
93 #else /* BSD */
94
95 int 
96 open_tun(const char *tun_device) 
97 {
98         int i;
99         int tun_fd;
100         char tun_name[50];
101
102         if (tun_device != NULL) {
103                 snprintf(tun_name, sizeof(tun_name), "/dev/%s", tun_device);
104                 strncpy(if_name, tun_device, sizeof(if_name));
105
106                 if ((tun_fd = open(tun_name, O_RDWR)) < 0) {
107                         warn("open_tun: %s: %s", tun_name, strerror(errno));
108                         return -1;
109                 }
110
111                 printf("Opened %s\n", tun_name);
112                 return tun_fd;
113         } else {
114                 for (i = 0; i < TUN_MAX_TRY; i++) {
115                         snprintf(tun_name, sizeof(tun_name), "/dev/tun%d", i);
116
117                         if ((tun_fd = open(tun_name, O_RDWR)) >= 0) {
118                                 printf("Opened %s\n", tun_name);
119                                 snprintf(if_name, sizeof(if_name), "tun%d", i);
120                                 return tun_fd;
121                         }
122
123                         if (errno == ENOENT)
124                                 break;
125                 }
126
127                 warn("open_tun: Failed to open tunneling device");
128         }
129
130         return -1;
131 }
132
133 #endif /* !LINUX */
134
135 void 
136 close_tun(int tun_fd) 
137 {
138         if (tun_fd >= 0)
139                 close(tun_fd);
140 }
141
142 int 
143 write_tun(int tun_fd, char *data, int len) 
144 {
145 #if defined (FREEBSD) || defined (DARWIN) || defined(NETBSD)
146         data += 4;
147         len -= 4;
148 #else /* !FREEBSD/DARWIN */
149 #ifdef LINUX
150         data[0] = 0x00;
151         data[1] = 0x00;
152         data[2] = 0x08;
153         data[3] = 0x00;
154 #else /* OPENBSD */
155         data[0] = 0x00;
156         data[1] = 0x00;
157         data[2] = 0x00;
158         data[3] = 0x02;
159 #endif /* !LINUX */
160 #endif /* FREEBSD */
161
162         if (write(tun_fd, data, len) != len) {
163                 warn("write_tun");
164                 return 1;
165         }
166         return 0;
167 }
168
169 int 
170 read_tun(int tun_fd, char *buf, int len) 
171 {
172 #if defined (FREEBSD) || defined (DARWIN) || defined(NETBSD)
173         /* FreeBSD/Darwin/NetBSD has no header */
174         return read(tun_fd, buf + 4, len - 4) + 4;
175 #else /* !FREEBSD */
176         return read(tun_fd, buf, len);
177 #endif /* !FREEBSD */
178 }
179
180 int
181 tun_setip(const char *ip)
182 {
183         char cmdline[512];
184
185         if (inet_addr(ip) != INADDR_NONE) {
186                 snprintf(cmdline, sizeof(cmdline), 
187                                 "/sbin/ifconfig %s %s %s netmask 255.255.255.0",
188                                 if_name,
189                                 ip,
190                                 ip);
191                 
192                 printf("Setting IP of %s to %s\n", if_name, ip);
193 #ifndef LINUX
194                 int r;
195
196                 r = system(cmdline);
197                 if(r != 0) {
198                         return r;
199                 } else {
200                         snprintf(cmdline, sizeof(cmdline),
201                                         "/sbin/route add %s/24 %s",
202                                         ip, ip);
203                 }
204                 printf("Adding route %s/24 to %s\n", ip, ip);
205 #endif
206                 return system(cmdline);
207         } else {
208                 printf("Invalid IP: %s!\n", ip);
209         }
210
211         return 1;
212 }
213
214 int 
215 tun_setmtu(const int mtu)
216 {
217         char cmdline[512];
218
219         if (mtu > 200 && mtu < 1500) {
220                 snprintf(cmdline, sizeof(cmdline), 
221                                 "/sbin/ifconfig %s mtu %d",
222                                 if_name,
223                                 mtu);
224                 
225                 printf("Setting MTU of %s to %d\n", if_name, mtu);
226                 return system(cmdline);
227         } else {
228                 warn("MTU out of range: %d\n", mtu);
229         }
230
231         return 1;
232 }
233