2 device.c -- Interaction BSD tun/tap device
3 Copyright (C) 2001-2005 Ivo Timmermans,
4 2001-2013 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.
22 #include "../system.h"
25 #include "../device.h"
26 #include "../logger.h"
31 #include "../xalloc.h"
34 #include "bsd/tunemu.h"
37 #define DEFAULT_TUN_DEVICE "/dev/tun0"
38 #if defined(HAVE_FREEBSD) || defined(HAVE_NETBSD)
39 #define DEFAULT_TAP_DEVICE "/dev/tap0"
41 #define DEFAULT_TAP_DEVICE "/dev/tun0"
44 typedef enum device_type {
46 DEVICE_TYPE_TUNIFHEAD,
56 static char *device_info = NULL;
57 static uint64_t device_total_in = 0;
58 static uint64_t device_total_out = 0;
59 #if defined(ENABLE_TUNEMU)
60 static device_type_t device_type = DEVICE_TYPE_TUNEMU;
61 #elif defined(HAVE_OPENBSD) || defined(HAVE_FREEBSD) || defined(HAVE_DRAGONFLY)
62 static device_type_t device_type = DEVICE_TYPE_TUNIFHEAD;
64 static device_type_t device_type = DEVICE_TYPE_TUN;
67 static bool setup_device(void) {
70 if(!get_config_string(lookup_config(config_tree, "Device"), &device)) {
71 if(routing_mode == RMODE_ROUTER)
72 device = xstrdup(DEFAULT_TUN_DEVICE);
74 device = xstrdup(DEFAULT_TAP_DEVICE);
77 if(!get_config_string(lookup_config(config_tree, "Interface"), &iface))
78 iface = xstrdup(strrchr(device, '/') ? strrchr(device, '/') + 1 : device);
80 if(get_config_string(lookup_config(config_tree, "DeviceType"), &type)) {
81 if(!strcasecmp(type, "tun"))
84 else if(!strcasecmp(type, "tunemu"))
85 device_type = DEVICE_TYPE_TUNEMU;
87 else if(!strcasecmp(type, "tunnohead"))
88 device_type = DEVICE_TYPE_TUN;
89 else if(!strcasecmp(type, "tunifhead"))
90 device_type = DEVICE_TYPE_TUNIFHEAD;
91 else if(!strcasecmp(type, "tap"))
92 device_type = DEVICE_TYPE_TAP;
94 logger(DEBUG_ALWAYS, LOG_ERR, "Unknown device type %s!", type);
98 if(strstr(device, "tap") || routing_mode != RMODE_ROUTER)
99 device_type = DEVICE_TYPE_TAP;
102 switch(device_type) {
104 case DEVICE_TYPE_TUNEMU: {
105 char dynamic_name[256] = "";
106 device_fd = tunemu_open(dynamic_name);
111 device_fd = open(device, O_RDWR | O_NONBLOCK);
115 logger(DEBUG_ALWAYS, LOG_ERR, "Could not open %s: %s", device, strerror(errno));
120 fcntl(device_fd, F_SETFD, FD_CLOEXEC);
123 switch(device_type) {
125 device_type = DEVICE_TYPE_TUN;
126 case DEVICE_TYPE_TUN:
130 if(ioctl(device_fd, TUNSIFHEAD, &zero, sizeof zero) == -1) {
131 logger(DEBUG_ALWAYS, LOG_ERR, "System call `%s' failed: %s", "ioctl", strerror(errno));
136 #if defined(TUNSIFMODE) && defined(IFF_BROADCAST) && defined(IFF_MULTICAST)
138 const int mode = IFF_BROADCAST | IFF_MULTICAST;
139 ioctl(device_fd, TUNSIFMODE, &mode, sizeof mode);
143 device_info = "Generic BSD tun device";
145 case DEVICE_TYPE_TUNIFHEAD:
149 if(ioctl(device_fd, TUNSIFHEAD, &one, sizeof one) == -1) {
150 logger(DEBUG_ALWAYS, LOG_ERR, "System call `%s' failed: %s", "ioctl", strerror(errno));
155 #if defined(TUNSIFMODE) && defined(IFF_BROADCAST) && defined(IFF_MULTICAST)
157 const int mode = IFF_BROADCAST | IFF_MULTICAST;
158 ioctl(device_fd, TUNSIFMODE, &mode, sizeof mode);
162 device_info = "Generic BSD tun device";
164 case DEVICE_TYPE_TAP:
165 if(routing_mode == RMODE_ROUTER)
166 overwrite_mac = true;
167 device_info = "Generic BSD tap device";
171 if(ioctl(device_fd, TAPGIFNAME, (void*)&ifr) == 0) {
174 iface = xstrdup(ifr.ifr_name);
181 case DEVICE_TYPE_TUNEMU:
182 device_info = "BSD tunemu device";
187 logger(DEBUG_ALWAYS, LOG_INFO, "%s is a %s", device, device_info);
192 static void close_device(void) {
193 switch(device_type) {
195 case DEVICE_TYPE_TUNEMU:
196 tunemu_close(device_fd);
207 static bool read_packet(vpn_packet_t *packet) {
210 switch(device_type) {
211 case DEVICE_TYPE_TUN:
213 case DEVICE_TYPE_TUNEMU:
214 if(device_type == DEVICE_TYPE_TUNEMU)
215 inlen = tunemu_read(device_fd, packet->data + 14, MTU - 14);
218 inlen = read(device_fd, packet->data + 14, MTU - 14);
221 logger(DEBUG_ALWAYS, LOG_ERR, "Error while reading from %s %s: %s", device_info,
222 device, strerror(errno));
226 switch(packet->data[14] >> 4) {
228 packet->data[12] = 0x08;
229 packet->data[13] = 0x00;
232 packet->data[12] = 0x86;
233 packet->data[13] = 0xDD;
236 logger(DEBUG_TRAFFIC, LOG_ERR,
237 "Unknown IP version %d while reading packet from %s %s",
238 packet->data[14] >> 4, device_info, device);
242 memset(packet->data, 0, 12);
243 packet->len = inlen + 14;
246 case DEVICE_TYPE_TUNIFHEAD: {
248 struct iovec vector[2] = {{&type, sizeof type}, {packet->data + 14, MTU - 14}};
250 if((inlen = readv(device_fd, vector, 2)) <= 0) {
251 logger(DEBUG_ALWAYS, LOG_ERR, "Error while reading from %s %s: %s", device_info,
252 device, strerror(errno));
256 switch (ntohl(type)) {
258 packet->data[12] = 0x08;
259 packet->data[13] = 0x00;
263 packet->data[12] = 0x86;
264 packet->data[13] = 0xDD;
268 logger(DEBUG_TRAFFIC, LOG_ERR,
269 "Unknown address family %x while reading packet from %s %s",
270 ntohl(type), device_info, device);
274 memset(packet->data, 0, 12);
275 packet->len = inlen + 10;
279 case DEVICE_TYPE_TAP:
280 if((inlen = read(device_fd, packet->data, MTU)) <= 0) {
281 logger(DEBUG_ALWAYS, LOG_ERR, "Error while reading from %s %s: %s", device_info,
282 device, strerror(errno));
293 device_total_in += packet->len;
295 logger(DEBUG_TRAFFIC, LOG_DEBUG, "Read packet of %d bytes from %s",
296 packet->len, device_info);
301 static bool write_packet(vpn_packet_t *packet) {
302 logger(DEBUG_TRAFFIC, LOG_DEBUG, "Writing packet of %d bytes to %s",
303 packet->len, device_info);
305 switch(device_type) {
306 case DEVICE_TYPE_TUN:
307 if(write(device_fd, packet->data + 14, packet->len - 14) < 0) {
308 logger(DEBUG_ALWAYS, LOG_ERR, "Error while writing to %s %s: %s", device_info,
309 device, strerror(errno));
314 case DEVICE_TYPE_TUNIFHEAD: {
316 struct iovec vector[2] = {{&type, sizeof type}, {packet->data + 14, packet->len - 14}};
319 af = (packet->data[12] << 8) + packet->data[13];
323 type = htonl(AF_INET);
326 type = htonl(AF_INET6);
329 logger(DEBUG_TRAFFIC, LOG_ERR,
330 "Unknown address family %x while writing packet to %s %s",
331 af, device_info, device);
335 if(writev(device_fd, vector, 2) < 0) {
336 logger(DEBUG_ALWAYS, LOG_ERR, "Can't write to %s %s: %s", device_info, device,
343 case DEVICE_TYPE_TAP:
344 if(write(device_fd, packet->data, packet->len) < 0) {
345 logger(DEBUG_ALWAYS, LOG_ERR, "Error while writing to %s %s: %s", device_info,
346 device, strerror(errno));
352 case DEVICE_TYPE_TUNEMU:
353 if(tunemu_write(device_fd, packet->data + 14, packet->len - 14) < 0) {
354 logger(DEBUG_ALWAYS, LOG_ERR, "Error while writing to %s %s: %s", device_info,
355 device, strerror(errno));
365 device_total_out += packet->len;
370 static void dump_device_stats(void) {
371 logger(DEBUG_ALWAYS, LOG_DEBUG, "Statistics for %s %s:", device_info, device);
372 logger(DEBUG_ALWAYS, LOG_DEBUG, " total bytes in: %10"PRIu64, device_total_in);
373 logger(DEBUG_ALWAYS, LOG_DEBUG, " total bytes out: %10"PRIu64, device_total_out);
376 const devops_t os_devops = {
377 .setup = setup_device,
378 .close = close_device,
380 .write = write_packet,
381 .dump_stats = dump_device_stats,