2 device.c -- Interaction BSD tun/tap device
3 Copyright (C) 2001-2005 Ivo Timmermans,
4 2001-2012 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.
33 #include "bsd/tunemu.h"
36 #define DEFAULT_TUN_DEVICE "/dev/tun0"
37 #if defined(HAVE_FREEBSD) || defined(HAVE_NETBSD)
38 #define DEFAULT_TAP_DEVICE "/dev/tap0"
40 #define DEFAULT_TAP_DEVICE "/dev/tun0"
43 typedef enum device_type {
45 DEVICE_TYPE_TUNIFHEAD,
55 static char *device_info = NULL;
56 static uint64_t device_total_in = 0;
57 static uint64_t device_total_out = 0;
59 static device_type_t device_type = DEVICE_TYPE_TUNEMU;
60 #elif defined(HAVE_OPENBSD) || defined(HAVE_FREEBSD) || defined(HAVE_DRAGONFLY)
61 static device_type_t device_type = DEVICE_TYPE_TUNIFHEAD;
63 static device_type_t device_type = DEVICE_TYPE_TUN;
66 static bool setup_device(void) {
69 if(!get_config_string(lookup_config(config_tree, "Device"), &device)) {
70 if(routing_mode == RMODE_ROUTER)
71 device = xstrdup(DEFAULT_TUN_DEVICE);
73 device = xstrdup(DEFAULT_TAP_DEVICE);
76 if(!get_config_string(lookup_config(config_tree, "Interface"), &iface))
77 iface = xstrdup(strrchr(device, '/') ? strrchr(device, '/') + 1 : device);
79 if(get_config_string(lookup_config(config_tree, "DeviceType"), &type)) {
80 if(!strcasecmp(type, "tun"))
83 else if(!strcasecmp(type, "tunemu"))
84 device_type = DEVICE_TYPE_TUNEMU;
86 else if(!strcasecmp(type, "tunnohead"))
87 device_type = DEVICE_TYPE_TUN;
88 else if(!strcasecmp(type, "tunifhead"))
89 device_type = DEVICE_TYPE_TUNIFHEAD;
90 else if(!strcasecmp(type, "tap"))
91 device_type = DEVICE_TYPE_TAP;
93 logger(DEBUG_ALWAYS, LOG_ERR, "Unknown device type %s!", type);
97 if(strstr(device, "tap") || routing_mode != RMODE_ROUTER)
98 device_type = DEVICE_TYPE_TAP;
101 switch(device_type) {
103 case DEVICE_TYPE_TUNEMU: {
104 char dynamic_name[256] = "";
105 device_fd = tunemu_open(dynamic_name);
110 device_fd = open(device, O_RDWR | O_NONBLOCK);
114 logger(DEBUG_ALWAYS, LOG_ERR, "Could not open %s: %s", device, strerror(errno));
119 fcntl(device_fd, F_SETFD, FD_CLOEXEC);
122 switch(device_type) {
124 device_type = DEVICE_TYPE_TUN;
125 case DEVICE_TYPE_TUN:
129 if(ioctl(device_fd, TUNSIFHEAD, &zero, sizeof zero) == -1) {
130 logger(DEBUG_ALWAYS, LOG_ERR, "System call `%s' failed: %s", "ioctl", strerror(errno));
135 #if defined(TUNSIFMODE) && defined(IFF_BROADCAST) && defined(IFF_MULTICAST)
137 const int mode = IFF_BROADCAST | IFF_MULTICAST;
138 ioctl(device_fd, TUNSIFMODE, &mode, sizeof mode);
142 device_info = "Generic BSD tun device";
144 case DEVICE_TYPE_TUNIFHEAD:
148 if(ioctl(device_fd, TUNSIFHEAD, &one, sizeof one) == -1) {
149 logger(DEBUG_ALWAYS, LOG_ERR, "System call `%s' failed: %s", "ioctl", strerror(errno));
154 #if defined(TUNSIFMODE) && defined(IFF_BROADCAST) && defined(IFF_MULTICAST)
156 const int mode = IFF_BROADCAST | IFF_MULTICAST;
157 ioctl(device_fd, TUNSIFMODE, &mode, sizeof mode);
161 device_info = "Generic BSD tun device";
163 case DEVICE_TYPE_TAP:
164 if(routing_mode == RMODE_ROUTER)
165 overwrite_mac = true;
166 device_info = "Generic BSD tap device";
170 if(ioctl(device_fd, TAPGIFNAME, (void*)&ifr) == 0) {
173 iface = xstrdup(ifr.ifr_name);
180 case DEVICE_TYPE_TUNEMU:
181 device_info = "BSD tunemu device";
186 logger(DEBUG_ALWAYS, LOG_INFO, "%s is a %s", device, device_info);
191 static void close_device(void) {
192 switch(device_type) {
194 case DEVICE_TYPE_TUNEMU:
195 tunemu_close(device_fd);
206 static bool read_packet(vpn_packet_t *packet) {
209 switch(device_type) {
210 case DEVICE_TYPE_TUN:
212 case DEVICE_TYPE_TUNEMU:
213 if(device_type == DEVICE_TYPE_TUNEMU)
214 inlen = tunemu_read(device_fd, packet->data + 14, MTU - 14);
217 inlen = read(device_fd, packet->data + 14, MTU - 14);
220 logger(DEBUG_ALWAYS, LOG_ERR, "Error while reading from %s %s: %s", device_info,
221 device, strerror(errno));
225 switch(packet->data[14] >> 4) {
227 packet->data[12] = 0x08;
228 packet->data[13] = 0x00;
231 packet->data[12] = 0x86;
232 packet->data[13] = 0xDD;
235 logger(DEBUG_TRAFFIC, LOG_ERR,
236 "Unknown IP version %d while reading packet from %s %s",
237 packet->data[14] >> 4, device_info, device);
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 packet->len = inlen + 10;
276 case DEVICE_TYPE_TAP:
277 if((inlen = read(device_fd, packet->data, MTU)) <= 0) {
278 logger(DEBUG_ALWAYS, LOG_ERR, "Error while reading from %s %s: %s", device_info,
279 device, strerror(errno));
290 device_total_in += packet->len;
292 logger(DEBUG_TRAFFIC, LOG_DEBUG, "Read packet of %d bytes from %s",
293 packet->len, device_info);
298 static bool write_packet(vpn_packet_t *packet) {
299 logger(DEBUG_TRAFFIC, 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(DEBUG_ALWAYS, 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 logger(DEBUG_TRAFFIC, 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(DEBUG_ALWAYS, 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(DEBUG_ALWAYS, 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(DEBUG_ALWAYS, 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(DEBUG_ALWAYS, LOG_DEBUG, "Statistics for %s %s:", device_info, device);
369 logger(DEBUG_ALWAYS, LOG_DEBUG, " total bytes in: %10"PRIu64, device_total_in);
370 logger(DEBUG_ALWAYS, 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,