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);
205 static bool read_packet(vpn_packet_t *packet) {
208 switch(device_type) {
209 case DEVICE_TYPE_TUN:
211 case DEVICE_TYPE_TUNEMU:
212 if(device_type == DEVICE_TYPE_TUNEMU)
213 inlen = tunemu_read(device_fd, packet->data + 14, MTU - 14);
216 inlen = read(device_fd, packet->data + 14, MTU - 14);
219 logger(DEBUG_ALWAYS, LOG_ERR, "Error while reading from %s %s: %s", device_info,
220 device, strerror(errno));
224 switch(packet->data[14] >> 4) {
226 packet->data[12] = 0x08;
227 packet->data[13] = 0x00;
230 packet->data[12] = 0x86;
231 packet->data[13] = 0xDD;
234 logger(DEBUG_TRAFFIC, LOG_ERR,
235 "Unknown IP version %d while reading packet from %s %s",
236 packet->data[14] >> 4, device_info, device);
240 memset(packet->data, 0, 12);
241 packet->len = inlen + 14;
244 case DEVICE_TYPE_TUNIFHEAD: {
246 struct iovec vector[2] = {{&type, sizeof type}, {packet->data + 14, MTU - 14}};
248 if((inlen = readv(device_fd, vector, 2)) <= 0) {
249 logger(DEBUG_ALWAYS, LOG_ERR, "Error while reading from %s %s: %s", device_info,
250 device, strerror(errno));
254 switch (ntohl(type)) {
256 packet->data[12] = 0x08;
257 packet->data[13] = 0x00;
261 packet->data[12] = 0x86;
262 packet->data[13] = 0xDD;
266 logger(DEBUG_TRAFFIC, LOG_ERR,
267 "Unknown address family %x while reading packet from %s %s",
268 ntohl(type), device_info, device);
272 memset(packet->data, 0, 12);
273 packet->len = inlen + 10;
277 case DEVICE_TYPE_TAP:
278 if((inlen = read(device_fd, packet->data, MTU)) <= 0) {
279 logger(DEBUG_ALWAYS, LOG_ERR, "Error while reading from %s %s: %s", device_info,
280 device, strerror(errno));
291 logger(DEBUG_TRAFFIC, LOG_DEBUG, "Read packet of %d bytes from %s",
292 packet->len, device_info);
297 static bool write_packet(vpn_packet_t *packet) {
298 logger(DEBUG_TRAFFIC, LOG_DEBUG, "Writing packet of %d bytes to %s",
299 packet->len, device_info);
301 switch(device_type) {
302 case DEVICE_TYPE_TUN:
303 if(write(device_fd, packet->data + 14, packet->len - 14) < 0) {
304 logger(DEBUG_ALWAYS, LOG_ERR, "Error while writing to %s %s: %s", device_info,
305 device, strerror(errno));
310 case DEVICE_TYPE_TUNIFHEAD: {
312 struct iovec vector[2] = {{&type, sizeof type}, {packet->data + 14, packet->len - 14}};
315 af = (packet->data[12] << 8) + packet->data[13];
319 type = htonl(AF_INET);
322 type = htonl(AF_INET6);
325 logger(DEBUG_TRAFFIC, LOG_ERR,
326 "Unknown address family %x while writing packet to %s %s",
327 af, device_info, device);
331 if(writev(device_fd, vector, 2) < 0) {
332 logger(DEBUG_ALWAYS, LOG_ERR, "Can't write to %s %s: %s", device_info, device,
339 case DEVICE_TYPE_TAP:
340 if(write(device_fd, packet->data, packet->len) < 0) {
341 logger(DEBUG_ALWAYS, LOG_ERR, "Error while writing to %s %s: %s", device_info,
342 device, strerror(errno));
348 case DEVICE_TYPE_TUNEMU:
349 if(tunemu_write(device_fd, packet->data + 14, packet->len - 14) < 0) {
350 logger(DEBUG_ALWAYS, LOG_ERR, "Error while writing to %s %s: %s", device_info,
351 device, strerror(errno));
364 const devops_t os_devops = {
365 .setup = setup_device,
366 .close = close_device,
368 .write = write_packet,