2 device.c -- Interaction BSD tun/tap device
3 Copyright (C) 2001-2005 Ivo Timmermans,
4 2001-2009 Guus Sliepen <guus@tinc-vpn.org>
5 2009 Grzegorz Dymarek <gregd72002@googlemail.com>
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License along
18 with this program; if not, write to the Free Software Foundation, Inc.,
19 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
32 #include "bsd/tunemu.h"
35 #define DEFAULT_DEVICE "/dev/tun0"
37 typedef enum device_type {
39 DEVICE_TYPE_TUNIFHEAD,
49 static char *device_info = NULL;
50 static int device_total_in = 0;
51 static int device_total_out = 0;
53 static device_type_t device_type = DEVICE_TYPE_TUNEMU;
54 #elif defined(HAVE_OPENBSD) || defined(HAVE_FREEBSD)
55 static device_type_t device_type = DEVICE_TYPE_TUNIFHEAD;
57 static device_type_t device_type = DEVICE_TYPE_TUN;
60 bool setup_device(void) {
63 if(!get_config_string(lookup_config(config_tree, "Device"), &device))
64 device = xstrdup(DEFAULT_DEVICE);
66 if(!get_config_string(lookup_config(config_tree, "Interface"), &iface))
67 iface = xstrdup(rindex(device, '/') ? rindex(device, '/') + 1 : device);
69 if(get_config_string(lookup_config(config_tree, "DeviceType"), &type)) {
70 if(!strcasecmp(type, "tun"))
73 else if(!strcasecmp(type, "tunemu"))
74 device_type = DEVICE_TYPE_TUNEMU;
76 else if(!strcasecmp(type, "tunnohead"))
77 device_type = DEVICE_TYPE_TUN;
78 else if(!strcasecmp(type, "tunifhead"))
79 device_type = DEVICE_TYPE_TUNIFHEAD;
80 else if(!strcasecmp(type, "tap"))
81 device_type = DEVICE_TYPE_TAP;
83 logger(LOG_ERR, "Unknown device type %s!", type);
87 if(strstr(device, "tap") || routing_mode != RMODE_ROUTER)
88 device_type = DEVICE_TYPE_TAP;
93 case DEVICE_TYPE_TUNEMU: {
94 char dynamic_name[256] = "";
95 device_fd = tunemu_open(dynamic_name);
100 device_fd = open(device, O_RDWR | O_NONBLOCK);
104 logger(LOG_ERR, "Could not open %s: %s", device, strerror(errno));
108 switch(device_type) {
110 device_type = DEVICE_TYPE_TUN;
111 case DEVICE_TYPE_TUN:
115 if(ioctl(device_fd, TUNSIFHEAD, &zero, sizeof zero) == -1) {
116 logger(LOG_ERR, "System call `%s' failed: %s", "ioctl", strerror(errno));
121 #if defined(TUNSIFMODE) && defined(IFF_BROADCAST) && defined(IFF_MULTICAST)
123 const int mode = IFF_BROADCAST | IFF_MULTICAST;
124 ioctl(device_fd, TUNSIFMODE, &mode, sizeof mode);
128 device_info = "Generic BSD tun device";
130 case DEVICE_TYPE_TUNIFHEAD:
134 if(ioctl(device_fd, TUNSIFHEAD, &one, sizeof one) == -1) {
135 logger(LOG_ERR, "System call `%s' failed: %s", "ioctl", strerror(errno));
140 #if defined(TUNSIFMODE) && defined(IFF_BROADCAST) && defined(IFF_MULTICAST)
142 const int mode = IFF_BROADCAST | IFF_MULTICAST;
143 ioctl(device_fd, TUNSIFMODE, &mode, sizeof mode);
147 device_info = "Generic BSD tun device";
149 case DEVICE_TYPE_TAP:
150 if(routing_mode == RMODE_ROUTER)
151 overwrite_mac = true;
152 device_info = "Generic BSD tap device";
155 case DEVICE_TYPE_TUNEMU:
156 device_info = "BSD tunemu device";
161 logger(LOG_INFO, "%s is a %s", device, device_info);
166 void close_device(void) {
167 switch(device_type) {
169 case DEVICE_TYPE_TUNEMU:
170 tunemu_close(device_fd);
181 bool read_packet(vpn_packet_t *packet) {
184 switch(device_type) {
185 case DEVICE_TYPE_TUN:
187 case DEVICE_TYPE_TUNEMU:
188 if(device_type == DEVICE_TYPE_TUNEMU)
189 lenin = tunemu_read(device_fd, packet->data + 14, MTU - 14);
192 lenin = read(device_fd, packet->data + 14, MTU - 14);
196 logger(LOG_ERR, "Error while reading from %s %s: %s", device_info,
197 device, strerror(errno));
201 switch(packet->data[14] >> 4) {
203 packet->data[12] = 0x08;
204 packet->data[13] = 0x00;
207 packet->data[12] = 0x86;
208 packet->data[13] = 0xDD;
211 ifdebug(TRAFFIC) logger(LOG_ERR,
212 "Unknown IP version %d while reading packet from %s %s",
213 packet->data[14] >> 4, device_info, device);
217 packet->len = lenin + 14;
220 case DEVICE_TYPE_TUNIFHEAD: {
222 struct iovec vector[2] = {{&type, sizeof(type)}, {packet->data + 14, MTU - 14}};
224 if((lenin = readv(device_fd, vector, 2)) <= 0) {
225 logger(LOG_ERR, "Error while reading from %s %s: %s", device_info,
226 device, strerror(errno));
230 switch (ntohl(type)) {
232 packet->data[12] = 0x08;
233 packet->data[13] = 0x00;
237 packet->data[12] = 0x86;
238 packet->data[13] = 0xDD;
242 ifdebug(TRAFFIC) logger(LOG_ERR,
243 "Unknown address family %x while reading packet from %s %s",
244 ntohl(type), device_info, device);
248 packet->len = lenin + 10;
252 case DEVICE_TYPE_TAP:
253 if((lenin = read(device_fd, packet->data, MTU)) <= 0) {
254 logger(LOG_ERR, "Error while reading from %s %s: %s", device_info,
255 device, strerror(errno));
266 device_total_in += packet->len;
268 ifdebug(TRAFFIC) logger(LOG_DEBUG, "Read packet of %d bytes from %s",
269 packet->len, device_info);
271 logger(LOG_INFO, "E:fd_read");
275 bool write_packet(vpn_packet_t *packet) {
276 ifdebug(TRAFFIC) logger(LOG_DEBUG, "Writing packet of %d bytes to %s",
277 packet->len, device_info);
279 switch(device_type) {
280 case DEVICE_TYPE_TUN:
281 if(write(device_fd, packet->data + 14, packet->len - 14) < 0) {
282 logger(LOG_ERR, "Error while writing to %s %s: %s", device_info,
283 device, strerror(errno));
288 case DEVICE_TYPE_TUNIFHEAD: {
290 struct iovec vector[2] = {{&type, sizeof(type)}, {packet->data + 14, packet->len - 14}};
293 af = (packet->data[12] << 8) + packet->data[13];
297 type = htonl(AF_INET);
300 type = htonl(AF_INET6);
303 ifdebug(TRAFFIC) logger(LOG_ERR,
304 "Unknown address family %x while writing packet to %s %s",
305 af, device_info, device);
309 if(writev(device_fd, vector, 2) < 0) {
310 logger(LOG_ERR, "Can't write to %s %s: %s", device_info, device,
317 case DEVICE_TYPE_TAP:
318 if(write(device_fd, packet->data, packet->len) < 0) {
319 logger(LOG_ERR, "Error while writing to %s %s: %s", device_info,
320 device, strerror(errno));
326 case DEVICE_TYPE_TUNEMU:
327 if(tunemu_write(device_fd, packet->data + 14, packet->len - 14) < 0) {
328 logger(LOG_ERR, "Error while writing to %s %s: %s", device_info,
329 device, strerror(errno));
339 device_total_out += packet->len;
344 void dump_device_stats(void) {
345 logger(LOG_DEBUG, "Statistics for %s %s:", device_info, device);
346 logger(LOG_DEBUG, " total bytes in: %10d", device_total_in);
347 logger(LOG_DEBUG, " total bytes out: %10d", device_total_out);