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_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) {
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))
79 logger(DEBUG_ALWAYS, LOG_WARNING, "Ignoring specified interface name '%s' as device rename is not supported on this platform", iface);
85 iface = xstrdup(strrchr(device, '/') ? strrchr(device, '/') + 1 : device);
87 if(get_config_string(lookup_config(config_tree, "DeviceType"), &type)) {
88 if(!strcasecmp(type, "tun"))
91 else if(!strcasecmp(type, "tunemu"))
92 device_type = DEVICE_TYPE_TUNEMU;
94 else if(!strcasecmp(type, "tunnohead"))
95 device_type = DEVICE_TYPE_TUN;
96 else if(!strcasecmp(type, "tunifhead"))
97 device_type = DEVICE_TYPE_TUNIFHEAD;
98 else if(!strcasecmp(type, "tap"))
99 device_type = DEVICE_TYPE_TAP;
101 logger(DEBUG_ALWAYS, LOG_ERR, "Unknown device type %s!", type);
105 if(strstr(device, "tap") || routing_mode != RMODE_ROUTER)
106 device_type = DEVICE_TYPE_TAP;
109 switch(device_type) {
111 case DEVICE_TYPE_TUNEMU: {
112 char dynamic_name[256] = "";
113 device_fd = tunemu_open(dynamic_name);
118 device_fd = open(device, O_RDWR | O_NONBLOCK);
122 logger(DEBUG_ALWAYS, LOG_ERR, "Could not open %s: %s", device, strerror(errno));
127 fcntl(device_fd, F_SETFD, FD_CLOEXEC);
130 switch(device_type) {
132 device_type = DEVICE_TYPE_TUN;
133 case DEVICE_TYPE_TUN:
137 if(ioctl(device_fd, TUNSIFHEAD, &zero, sizeof zero) == -1) {
138 logger(DEBUG_ALWAYS, LOG_ERR, "System call `%s' failed: %s", "ioctl", strerror(errno));
143 #if defined(TUNSIFMODE) && defined(IFF_BROADCAST) && defined(IFF_MULTICAST)
145 const int mode = IFF_BROADCAST | IFF_MULTICAST;
146 ioctl(device_fd, TUNSIFMODE, &mode, sizeof mode);
150 device_info = "Generic BSD tun device";
152 case DEVICE_TYPE_TUNIFHEAD:
156 if(ioctl(device_fd, TUNSIFHEAD, &one, sizeof one) == -1) {
157 logger(DEBUG_ALWAYS, LOG_ERR, "System call `%s' failed: %s", "ioctl", strerror(errno));
162 #if defined(TUNSIFMODE) && defined(IFF_BROADCAST) && defined(IFF_MULTICAST)
164 const int mode = IFF_BROADCAST | IFF_MULTICAST;
165 ioctl(device_fd, TUNSIFMODE, &mode, sizeof mode);
169 device_info = "Generic BSD tun device";
171 case DEVICE_TYPE_TAP:
172 if(routing_mode == RMODE_ROUTER)
173 overwrite_mac = true;
174 device_info = "Generic BSD tap device";
178 if(ioctl(device_fd, TAPGIFNAME, (void*)&ifr) == 0) {
181 iface = xstrdup(ifr.ifr_name);
188 case DEVICE_TYPE_TUNEMU:
189 device_info = "BSD tunemu device";
194 logger(DEBUG_ALWAYS, LOG_INFO, "%s is a %s", device, device_info);
199 static void close_device(void) {
200 switch(device_type) {
202 case DEVICE_TYPE_TUNEMU:
203 tunemu_close(device_fd);
211 free(device); device = NULL;
212 free(iface); iface = NULL;
216 static bool read_packet(vpn_packet_t *packet) {
219 switch(device_type) {
220 case DEVICE_TYPE_TUN:
222 case DEVICE_TYPE_TUNEMU:
223 if(device_type == DEVICE_TYPE_TUNEMU)
224 inlen = tunemu_read(device_fd, packet->data + 14, MTU - 14);
227 inlen = read(device_fd, packet->data + 14, MTU - 14);
230 logger(DEBUG_ALWAYS, LOG_ERR, "Error while reading from %s %s: %s", device_info,
231 device, strerror(errno));
235 switch(packet->data[14] >> 4) {
237 packet->data[12] = 0x08;
238 packet->data[13] = 0x00;
241 packet->data[12] = 0x86;
242 packet->data[13] = 0xDD;
245 logger(DEBUG_TRAFFIC, LOG_ERR,
246 "Unknown IP version %d while reading packet from %s %s",
247 packet->data[14] >> 4, device_info, device);
251 memset(packet->data, 0, 12);
252 packet->len = inlen + 14;
255 case DEVICE_TYPE_TUNIFHEAD: {
257 struct iovec vector[2] = {{&type, sizeof type}, {packet->data + 14, MTU - 14}};
259 if((inlen = readv(device_fd, vector, 2)) <= 0) {
260 logger(DEBUG_ALWAYS, LOG_ERR, "Error while reading from %s %s: %s", device_info,
261 device, strerror(errno));
265 switch (ntohl(type)) {
267 packet->data[12] = 0x08;
268 packet->data[13] = 0x00;
272 packet->data[12] = 0x86;
273 packet->data[13] = 0xDD;
277 logger(DEBUG_TRAFFIC, LOG_ERR,
278 "Unknown address family %x while reading packet from %s %s",
279 ntohl(type), device_info, device);
283 memset(packet->data, 0, 12);
284 packet->len = inlen + 10;
288 case DEVICE_TYPE_TAP:
289 if((inlen = read(device_fd, packet->data, MTU)) <= 0) {
290 logger(DEBUG_ALWAYS, LOG_ERR, "Error while reading from %s %s: %s", device_info,
291 device, strerror(errno));
302 logger(DEBUG_TRAFFIC, LOG_DEBUG, "Read packet of %d bytes from %s",
303 packet->len, device_info);
308 static bool write_packet(vpn_packet_t *packet) {
309 logger(DEBUG_TRAFFIC, LOG_DEBUG, "Writing packet of %d bytes to %s",
310 packet->len, device_info);
312 switch(device_type) {
313 case DEVICE_TYPE_TUN:
314 if(write(device_fd, packet->data + 14, packet->len - 14) < 0) {
315 logger(DEBUG_ALWAYS, LOG_ERR, "Error while writing to %s %s: %s", device_info,
316 device, strerror(errno));
321 case DEVICE_TYPE_TUNIFHEAD: {
323 struct iovec vector[2] = {{&type, sizeof type}, {packet->data + 14, packet->len - 14}};
326 af = (packet->data[12] << 8) + packet->data[13];
330 type = htonl(AF_INET);
333 type = htonl(AF_INET6);
336 logger(DEBUG_TRAFFIC, LOG_ERR,
337 "Unknown address family %x while writing packet to %s %s",
338 af, device_info, device);
342 if(writev(device_fd, vector, 2) < 0) {
343 logger(DEBUG_ALWAYS, LOG_ERR, "Can't write to %s %s: %s", device_info, device,
350 case DEVICE_TYPE_TAP:
351 if(write(device_fd, packet->data, packet->len) < 0) {
352 logger(DEBUG_ALWAYS, LOG_ERR, "Error while writing to %s %s: %s", device_info,
353 device, strerror(errno));
359 case DEVICE_TYPE_TUNEMU:
360 if(tunemu_write(device_fd, packet->data + 14, packet->len - 14) < 0) {
361 logger(DEBUG_ALWAYS, LOG_ERR, "Error while writing to %s %s: %s", device_info,
362 device, strerror(errno));
375 const devops_t os_devops = {
376 .setup = setup_device,
377 .close = close_device,
379 .write = write_packet,