New upstream release:
[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                 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)
185 {
186         char cmdline[512];
187 #ifndef LINUX
188                 int r;
189 #endif
190
191         if (inet_addr(ip) != INADDR_NONE) {
192                 snprintf(cmdline, sizeof(cmdline), 
193                                 "/sbin/ifconfig %s %s %s netmask 255.255.255.0",
194                                 if_name,
195                                 ip,
196                                 ip);
197                 
198                 printf("Setting IP of %s to %s\n", if_name, ip);
199 #ifndef LINUX
200                 r = system(cmdline);
201                 if(r != 0) {
202                         return r;
203                 } else {
204                         snprintf(cmdline, sizeof(cmdline),
205                                         "/sbin/route add %s/24 %s",
206                                         ip, ip);
207                 }
208                 printf("Adding route %s/24 to %s\n", ip, ip);
209 #endif
210                 return system(cmdline);
211         } else {
212                 printf("Invalid IP: %s!\n", ip);
213         }
214
215         return 1;
216 }
217
218 int 
219 tun_setmtu(const size_t mtu)
220 {
221         char cmdline[512];
222
223         if (mtu > 200 && mtu < 1500) {
224                 snprintf(cmdline, sizeof(cmdline), 
225                                 "/sbin/ifconfig %s mtu %u",
226                                 if_name,
227                                 mtu);
228                 
229                 printf("Setting MTU of %s to %u\n", if_name, mtu);
230                 return system(cmdline);
231         } else {
232                 warn("MTU out of range: %u\n", mtu);
233         }
234
235         return 1;
236 }
237