2 device.c -- Interaction BSD tun/tap device
3 Copyright (C) 2001-2005 Ivo Timmermans,
4 2001-2014 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_DARWIN) || 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) {
66 get_config_string(lookup_config(config_tree, "Device"), &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(DEBUG_ALWAYS, LOG_ERR, "Unknown device type %s!", type);
87 if((device && strstr(device, "tap")) || routing_mode != RMODE_ROUTER)
88 device_type = DEVICE_TYPE_TAP;
92 if(device_type == DEVICE_TYPE_TAP)
93 device = xstrdup(DEFAULT_TAP_DEVICE);
95 device = xstrdup(DEFAULT_TUN_DEVICE);
98 if(!get_config_string(lookup_config(config_tree, "Interface"), &iface))
102 logger(DEBUG_ALWAYS, LOG_WARNING, "Ignoring specified interface name '%s' as device rename is not supported on this platform", iface);
108 iface = xstrdup(strrchr(device, '/') ? strrchr(device, '/') + 1 : device);
110 switch(device_type) {
112 case DEVICE_TYPE_TUNEMU: {
113 char dynamic_name[256] = "";
114 device_fd = tunemu_open(dynamic_name);
119 device_fd = open(device, O_RDWR | O_NONBLOCK);
123 logger(DEBUG_ALWAYS, LOG_ERR, "Could not open %s: %s", device, strerror(errno));
128 fcntl(device_fd, F_SETFD, FD_CLOEXEC);
131 switch(device_type) {
133 device_type = DEVICE_TYPE_TUN;
134 case DEVICE_TYPE_TUN:
138 if(ioctl(device_fd, TUNSIFHEAD, &zero, sizeof zero) == -1) {
139 logger(DEBUG_ALWAYS, LOG_ERR, "System call `%s' failed: %s", "ioctl", strerror(errno));
144 #if defined(TUNSIFMODE) && defined(IFF_BROADCAST) && defined(IFF_MULTICAST)
146 const int mode = IFF_BROADCAST | IFF_MULTICAST;
147 ioctl(device_fd, TUNSIFMODE, &mode, sizeof mode);
151 device_info = "Generic BSD tun device";
153 case DEVICE_TYPE_TUNIFHEAD:
157 if(ioctl(device_fd, TUNSIFHEAD, &one, sizeof one) == -1) {
158 logger(DEBUG_ALWAYS, LOG_ERR, "System call `%s' failed: %s", "ioctl", strerror(errno));
163 #if defined(TUNSIFMODE) && defined(IFF_BROADCAST) && defined(IFF_MULTICAST)
165 const int mode = IFF_BROADCAST | IFF_MULTICAST;
166 ioctl(device_fd, TUNSIFMODE, &mode, sizeof mode);
170 device_info = "Generic BSD tun device";
172 case DEVICE_TYPE_TAP:
173 if(routing_mode == RMODE_ROUTER)
174 overwrite_mac = true;
175 device_info = "Generic BSD tap device";
179 if(ioctl(device_fd, TAPGIFNAME, (void*)&ifr) == 0) {
182 iface = xstrdup(ifr.ifr_name);
189 case DEVICE_TYPE_TUNEMU:
190 device_info = "BSD tunemu device";
195 logger(DEBUG_ALWAYS, LOG_INFO, "%s is a %s", device, device_info);
200 static void close_device(void) {
201 switch(device_type) {
203 case DEVICE_TYPE_TUNEMU:
204 tunemu_close(device_fd);
212 free(device); device = NULL;
213 free(iface); iface = NULL;
217 static bool read_packet(vpn_packet_t *packet) {
220 switch(device_type) {
221 case DEVICE_TYPE_TUN:
223 case DEVICE_TYPE_TUNEMU:
224 if(device_type == DEVICE_TYPE_TUNEMU)
225 inlen = tunemu_read(device_fd, DATA(packet) + 14, MTU - 14);
228 inlen = read(device_fd, DATA(packet) + 14, MTU - 14);
231 logger(DEBUG_ALWAYS, LOG_ERR, "Error while reading from %s %s: %s", device_info,
232 device, strerror(errno));
236 switch(DATA(packet)[14] >> 4) {
238 DATA(packet)[12] = 0x08;
239 DATA(packet)[13] = 0x00;
242 DATA(packet)[12] = 0x86;
243 DATA(packet)[13] = 0xDD;
246 logger(DEBUG_TRAFFIC, LOG_ERR,
247 "Unknown IP version %d while reading packet from %s %s",
248 DATA(packet)[14] >> 4, device_info, device);
252 memset(DATA(packet), 0, 12);
253 packet->len = inlen + 14;
256 case DEVICE_TYPE_TUNIFHEAD: {
258 struct iovec vector[2] = {{&type, sizeof type}, {DATA(packet) + 14, MTU - 14}};
260 if((inlen = readv(device_fd, vector, 2)) <= 0) {
261 logger(DEBUG_ALWAYS, LOG_ERR, "Error while reading from %s %s: %s", device_info,
262 device, strerror(errno));
266 switch (ntohl(type)) {
268 DATA(packet)[12] = 0x08;
269 DATA(packet)[13] = 0x00;
273 DATA(packet)[12] = 0x86;
274 DATA(packet)[13] = 0xDD;
278 logger(DEBUG_TRAFFIC, LOG_ERR,
279 "Unknown address family %x while reading packet from %s %s",
280 ntohl(type), device_info, device);
284 memset(DATA(packet), 0, 12);
285 packet->len = inlen + 10;
289 case DEVICE_TYPE_TAP:
290 if((inlen = read(device_fd, DATA(packet), MTU)) <= 0) {
291 logger(DEBUG_ALWAYS, LOG_ERR, "Error while reading from %s %s: %s", device_info,
292 device, strerror(errno));
303 logger(DEBUG_TRAFFIC, LOG_DEBUG, "Read packet of %d bytes from %s",
304 packet->len, device_info);
309 static bool write_packet(vpn_packet_t *packet) {
310 logger(DEBUG_TRAFFIC, LOG_DEBUG, "Writing packet of %d bytes to %s",
311 packet->len, device_info);
313 switch(device_type) {
314 case DEVICE_TYPE_TUN:
315 if(write(device_fd, DATA(packet) + 14, packet->len - 14) < 0) {
316 logger(DEBUG_ALWAYS, LOG_ERR, "Error while writing to %s %s: %s", device_info,
317 device, strerror(errno));
322 case DEVICE_TYPE_TUNIFHEAD: {
324 struct iovec vector[2] = {{&type, sizeof type}, {DATA(packet) + 14, packet->len - 14}};
327 af = (DATA(packet)[12] << 8) + DATA(packet)[13];
331 type = htonl(AF_INET);
334 type = htonl(AF_INET6);
337 logger(DEBUG_TRAFFIC, LOG_ERR,
338 "Unknown address family %x while writing packet to %s %s",
339 af, device_info, device);
343 if(writev(device_fd, vector, 2) < 0) {
344 logger(DEBUG_ALWAYS, LOG_ERR, "Can't write to %s %s: %s", device_info, device,
351 case DEVICE_TYPE_TAP:
352 if(write(device_fd, DATA(packet), packet->len) < 0) {
353 logger(DEBUG_ALWAYS, LOG_ERR, "Error while writing to %s %s: %s", device_info,
354 device, strerror(errno));
360 case DEVICE_TYPE_TUNEMU:
361 if(tunemu_write(device_fd, DATA(packet) + 14, packet->len - 14) < 0) {
362 logger(DEBUG_ALWAYS, LOG_ERR, "Error while writing to %s %s: %s", device_info,
363 device, strerror(errno));
376 const devops_t os_devops = {
377 .setup = setup_device,
378 .close = close_device,
380 .write = write_packet,