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 #if defined(ENABLE_TUNEMU)
58 static device_type_t device_type = DEVICE_TYPE_TUNEMU;
59 #elif defined(HAVE_OPENBSD) || defined(HAVE_FREEBSD) || defined(HAVE_DRAGONFLY)
60 static device_type_t device_type = DEVICE_TYPE_TUNIFHEAD;
62 static device_type_t device_type = DEVICE_TYPE_TUN;
65 static bool setup_device(void) {
68 if(!get_config_string(lookup_config(config_tree, "Device"), &device)) {
69 if(routing_mode == RMODE_ROUTER)
70 device = xstrdup(DEFAULT_TUN_DEVICE);
72 device = xstrdup(DEFAULT_TAP_DEVICE);
75 if(!get_config_string(lookup_config(config_tree, "Interface"), &iface))
76 iface = xstrdup(strrchr(device, '/') ? strrchr(device, '/') + 1 : device);
78 if(get_config_string(lookup_config(config_tree, "DeviceType"), &type)) {
79 if(!strcasecmp(type, "tun"))
82 else if(!strcasecmp(type, "tunemu"))
83 device_type = DEVICE_TYPE_TUNEMU;
85 else if(!strcasecmp(type, "tunnohead"))
86 device_type = DEVICE_TYPE_TUN;
87 else if(!strcasecmp(type, "tunifhead"))
88 device_type = DEVICE_TYPE_TUNIFHEAD;
89 else if(!strcasecmp(type, "tap"))
90 device_type = DEVICE_TYPE_TAP;
92 logger(DEBUG_ALWAYS, LOG_ERR, "Unknown device type %s!", type);
96 if(strstr(device, "tap") || routing_mode != RMODE_ROUTER)
97 device_type = DEVICE_TYPE_TAP;
100 switch(device_type) {
102 case DEVICE_TYPE_TUNEMU: {
103 char dynamic_name[256] = "";
104 device_fd = tunemu_open(dynamic_name);
109 device_fd = open(device, O_RDWR | O_NONBLOCK);
113 logger(DEBUG_ALWAYS, LOG_ERR, "Could not open %s: %s", device, strerror(errno));
118 fcntl(device_fd, F_SETFD, FD_CLOEXEC);
121 switch(device_type) {
123 device_type = DEVICE_TYPE_TUN;
124 case DEVICE_TYPE_TUN:
128 if(ioctl(device_fd, TUNSIFHEAD, &zero, sizeof zero) == -1) {
129 logger(DEBUG_ALWAYS, LOG_ERR, "System call `%s' failed: %s", "ioctl", strerror(errno));
134 #if defined(TUNSIFMODE) && defined(IFF_BROADCAST) && defined(IFF_MULTICAST)
136 const int mode = IFF_BROADCAST | IFF_MULTICAST;
137 ioctl(device_fd, TUNSIFMODE, &mode, sizeof mode);
141 device_info = "Generic BSD tun device";
143 case DEVICE_TYPE_TUNIFHEAD:
147 if(ioctl(device_fd, TUNSIFHEAD, &one, sizeof one) == -1) {
148 logger(DEBUG_ALWAYS, LOG_ERR, "System call `%s' failed: %s", "ioctl", strerror(errno));
153 #if defined(TUNSIFMODE) && defined(IFF_BROADCAST) && defined(IFF_MULTICAST)
155 const int mode = IFF_BROADCAST | IFF_MULTICAST;
156 ioctl(device_fd, TUNSIFMODE, &mode, sizeof mode);
160 device_info = "Generic BSD tun device";
162 case DEVICE_TYPE_TAP:
163 if(routing_mode == RMODE_ROUTER)
164 overwrite_mac = true;
165 device_info = "Generic BSD tap device";
169 if(ioctl(device_fd, TAPGIFNAME, (void*)&ifr) == 0) {
172 iface = xstrdup(ifr.ifr_name);
179 case DEVICE_TYPE_TUNEMU:
180 device_info = "BSD tunemu device";
185 logger(DEBUG_ALWAYS, LOG_INFO, "%s is a %s", device, device_info);
190 static void close_device(void) {
191 switch(device_type) {
193 case DEVICE_TYPE_TUNEMU:
194 tunemu_close(device_fd);
202 free(device); device = NULL;
203 free(iface); iface = NULL;
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 logger(DEBUG_TRAFFIC, LOG_DEBUG, "Read packet of %d bytes from %s",
294 packet->len, device_info);
299 static bool write_packet(vpn_packet_t *packet) {
300 logger(DEBUG_TRAFFIC, LOG_DEBUG, "Writing packet of %d bytes to %s",
301 packet->len, device_info);
303 switch(device_type) {
304 case DEVICE_TYPE_TUN:
305 if(write(device_fd, packet->data + 14, packet->len - 14) < 0) {
306 logger(DEBUG_ALWAYS, LOG_ERR, "Error while writing to %s %s: %s", device_info,
307 device, strerror(errno));
312 case DEVICE_TYPE_TUNIFHEAD: {
314 struct iovec vector[2] = {{&type, sizeof type}, {packet->data + 14, packet->len - 14}};
317 af = (packet->data[12] << 8) + packet->data[13];
321 type = htonl(AF_INET);
324 type = htonl(AF_INET6);
327 logger(DEBUG_TRAFFIC, LOG_ERR,
328 "Unknown address family %x while writing packet to %s %s",
329 af, device_info, device);
333 if(writev(device_fd, vector, 2) < 0) {
334 logger(DEBUG_ALWAYS, LOG_ERR, "Can't write to %s %s: %s", device_info, device,
341 case DEVICE_TYPE_TAP:
342 if(write(device_fd, packet->data, packet->len) < 0) {
343 logger(DEBUG_ALWAYS, LOG_ERR, "Error while writing to %s %s: %s", device_info,
344 device, strerror(errno));
350 case DEVICE_TYPE_TUNEMU:
351 if(tunemu_write(device_fd, packet->data + 14, packet->len - 14) < 0) {
352 logger(DEBUG_ALWAYS, LOG_ERR, "Error while writing to %s %s: %s", device_info,
353 device, strerror(errno));
366 const devops_t os_devops = {
367 .setup = setup_device,
368 .close = close_device,
370 .write = write_packet,