2 device.c -- Interaction BSD tun/tap device
3 Copyright (C) 2001-2005 Ivo Timmermans,
4 2001-2016 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"
30 #include "../xalloc.h"
36 #define DEFAULT_TUN_DEVICE "/dev/tun0"
37 #define DEFAULT_TAP_DEVICE "/dev/tap0"
39 typedef enum device_type {
41 DEVICE_TYPE_TUNIFHEAD,
51 static char *device_info = NULL;
52 static uint64_t device_total_in = 0;
53 static uint64_t device_total_out = 0;
54 #if defined(ENABLE_TUNEMU)
55 static device_type_t device_type = DEVICE_TYPE_TUNEMU;
56 #elif defined(HAVE_OPENBSD) || defined(HAVE_FREEBSD) || defined(HAVE_DRAGONFLY)
57 static device_type_t device_type = DEVICE_TYPE_TUNIFHEAD;
59 static device_type_t device_type = DEVICE_TYPE_TUN;
62 static bool setup_device(void) {
65 if(!get_config_string(lookup_config(config_tree, "Device"), &device)) {
66 if(routing_mode == RMODE_ROUTER)
67 device = xstrdup(DEFAULT_TUN_DEVICE);
69 device = xstrdup(DEFAULT_TAP_DEVICE);
72 if(!get_config_string(lookup_config(config_tree, "Interface"), &iface))
73 iface = xstrdup(strrchr(device, '/') ? strrchr(device, '/') + 1 : device);
74 else if(strcmp(iface, strrchr(device, '/') ? strrchr(device, '/') + 1 : device))
75 logger(LOG_WARNING, "Warning: Interface does not match Device. $INTERFACE might be set incorrectly.");
77 if(get_config_string(lookup_config(config_tree, "DeviceType"), &type)) {
78 if(!strcasecmp(type, "tun"))
81 else if(!strcasecmp(type, "tunemu"))
82 device_type = DEVICE_TYPE_TUNEMU;
84 else if(!strcasecmp(type, "tunnohead"))
85 device_type = DEVICE_TYPE_TUN;
86 else if(!strcasecmp(type, "tunifhead"))
87 device_type = DEVICE_TYPE_TUNIFHEAD;
88 else if(!strcasecmp(type, "tap"))
89 device_type = DEVICE_TYPE_TAP;
91 logger(LOG_ERR, "Unknown device type %s!", type);
95 if(strstr(device, "tap") || routing_mode != RMODE_ROUTER)
96 device_type = DEVICE_TYPE_TAP;
101 case DEVICE_TYPE_TUNEMU: {
102 char dynamic_name[256] = "";
103 device_fd = tunemu_open(dynamic_name);
108 device_fd = open(device, O_RDWR | O_NONBLOCK);
112 logger(LOG_ERR, "Could not open %s: %s", device, strerror(errno));
117 fcntl(device_fd, F_SETFD, FD_CLOEXEC);
120 switch(device_type) {
122 device_type = DEVICE_TYPE_TUN;
123 case DEVICE_TYPE_TUN:
127 if(ioctl(device_fd, TUNSIFHEAD, &zero, sizeof zero) == -1) {
128 logger(LOG_ERR, "System call `%s' failed: %s", "ioctl", strerror(errno));
133 #if defined(TUNSIFMODE) && defined(IFF_BROADCAST) && defined(IFF_MULTICAST)
135 const int mode = IFF_BROADCAST | IFF_MULTICAST;
136 ioctl(device_fd, TUNSIFMODE, &mode, sizeof mode);
140 device_info = "Generic BSD tun device";
142 case DEVICE_TYPE_TUNIFHEAD:
146 if(ioctl(device_fd, TUNSIFHEAD, &one, sizeof one) == -1) {
147 logger(LOG_ERR, "System call `%s' failed: %s", "ioctl", strerror(errno));
152 #if defined(TUNSIFMODE) && defined(IFF_BROADCAST) && defined(IFF_MULTICAST)
154 const int mode = IFF_BROADCAST | IFF_MULTICAST;
155 ioctl(device_fd, TUNSIFMODE, &mode, sizeof mode);
159 device_info = "Generic BSD tun device";
161 case DEVICE_TYPE_TAP:
162 if(routing_mode == RMODE_ROUTER)
163 overwrite_mac = true;
164 device_info = "Generic BSD tap device";
168 if(ioctl(device_fd, TAPGIFNAME, (void*)&ifr) == 0) {
171 iface = xstrdup(ifr.ifr_name);
178 case DEVICE_TYPE_TUNEMU:
179 device_info = "BSD tunemu device";
184 logger(LOG_INFO, "%s is a %s", device, device_info);
189 static void close_device(void) {
190 switch(device_type) {
192 case DEVICE_TYPE_TUNEMU:
193 tunemu_close(device_fd);
204 static bool read_packet(vpn_packet_t *packet) {
207 switch(device_type) {
208 case DEVICE_TYPE_TUN:
210 case DEVICE_TYPE_TUNEMU:
211 if(device_type == DEVICE_TYPE_TUNEMU)
212 lenin = tunemu_read(device_fd, packet->data + 14, MTU - 14);
215 lenin = read(device_fd, packet->data + 14, MTU - 14);
218 logger(LOG_ERR, "Error while reading from %s %s: %s", device_info,
219 device, strerror(errno));
223 switch(packet->data[14] >> 4) {
225 packet->data[12] = 0x08;
226 packet->data[13] = 0x00;
229 packet->data[12] = 0x86;
230 packet->data[13] = 0xDD;
233 ifdebug(TRAFFIC) logger(LOG_ERR,
234 "Unknown IP version %d while reading packet from %s %s",
235 packet->data[14] >> 4, device_info, device);
239 memset(packet->data, 0, 12);
240 packet->len = lenin + 14;
243 case DEVICE_TYPE_TUNIFHEAD: {
245 struct iovec vector[2] = {{&type, sizeof(type)}, {packet->data + 14, MTU - 14}};
247 if((lenin = readv(device_fd, vector, 2)) <= 0) {
248 logger(LOG_ERR, "Error while reading from %s %s: %s", device_info,
249 device, strerror(errno));
253 switch (ntohl(type)) {
255 packet->data[12] = 0x08;
256 packet->data[13] = 0x00;
260 packet->data[12] = 0x86;
261 packet->data[13] = 0xDD;
265 ifdebug(TRAFFIC) logger(LOG_ERR,
266 "Unknown address family %x while reading packet from %s %s",
267 ntohl(type), device_info, device);
271 memset(packet->data, 0, 12);
272 packet->len = lenin + 10;
276 case DEVICE_TYPE_TAP:
277 if((lenin = read(device_fd, packet->data, MTU)) <= 0) {
278 logger(LOG_ERR, "Error while reading from %s %s: %s", device_info,
279 device, strerror(errno));
290 device_total_in += packet->len;
292 ifdebug(TRAFFIC) logger(LOG_DEBUG, "Read packet of %d bytes from %s",
293 packet->len, device_info);
298 static bool write_packet(vpn_packet_t *packet) {
299 ifdebug(TRAFFIC) logger(LOG_DEBUG, "Writing packet of %d bytes to %s",
300 packet->len, device_info);
302 switch(device_type) {
303 case DEVICE_TYPE_TUN:
304 if(write(device_fd, packet->data + 14, packet->len - 14) < 0) {
305 logger(LOG_ERR, "Error while writing to %s %s: %s", device_info,
306 device, strerror(errno));
311 case DEVICE_TYPE_TUNIFHEAD: {
313 struct iovec vector[2] = {{&type, sizeof(type)}, {packet->data + 14, packet->len - 14}};
316 af = (packet->data[12] << 8) + packet->data[13];
320 type = htonl(AF_INET);
323 type = htonl(AF_INET6);
326 ifdebug(TRAFFIC) logger(LOG_ERR,
327 "Unknown address family %x while writing packet to %s %s",
328 af, device_info, device);
332 if(writev(device_fd, vector, 2) < 0) {
333 logger(LOG_ERR, "Can't write to %s %s: %s", device_info, device,
340 case DEVICE_TYPE_TAP:
341 if(write(device_fd, packet->data, packet->len) < 0) {
342 logger(LOG_ERR, "Error while writing to %s %s: %s", device_info,
343 device, strerror(errno));
349 case DEVICE_TYPE_TUNEMU:
350 if(tunemu_write(device_fd, packet->data + 14, packet->len - 14) < 0) {
351 logger(LOG_ERR, "Error while writing to %s %s: %s", device_info,
352 device, strerror(errno));
362 device_total_out += packet->len;
367 static void dump_device_stats(void) {
368 logger(LOG_DEBUG, "Statistics for %s %s:", device_info, device);
369 logger(LOG_DEBUG, " total bytes in: %10"PRIu64, device_total_in);
370 logger(LOG_DEBUG, " total bytes out: %10"PRIu64, device_total_out);
373 const devops_t os_devops = {
374 .setup = setup_device,
375 .close = close_device,
377 .write = write_packet,
378 .dump_stats = dump_device_stats,