2 device.c -- Interaction BSD tun/tap device
3 Copyright (C) 2001-2005 Ivo Timmermans,
4 2001-2009 Guus Sliepen <guus@tinc-vpn.org>
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 $Id: device.c 1398 2004-11-01 15:18:53Z guus $
32 #define DEFAULT_DEVICE "/dev/tun0"
34 typedef enum device_type {
36 DEVICE_TYPE_TUNIFHEAD,
43 static char *device_info = NULL;
44 static int device_total_in = 0;
45 static int device_total_out = 0;
46 #if defined(HAVE_OPENBSD) || defined(HAVE_FREEBSD)
47 static device_type_t device_type = DEVICE_TYPE_TUNIFHEAD;
49 static device_type_t device_type = DEVICE_TYPE_TUN;
52 bool setup_device(void) {
57 if(!get_config_string(lookup_config(config_tree, "Device"), &device))
58 device = xstrdup(DEFAULT_DEVICE);
60 if(!get_config_string(lookup_config(config_tree, "Interface"), &iface))
61 iface = xstrdup(rindex(device, '/') ? rindex(device, '/') + 1 : device);
63 if((device_fd = open(device, O_RDWR | O_NONBLOCK)) < 0) {
64 logger(LOG_ERR, _("Could not open %s: %s"), device, strerror(errno));
68 if(get_config_string(lookup_config(config_tree, "DeviceType"), &type)) {
69 if(!strcasecmp(type, "tun"))
71 else if(!strcasecmp(type, "tunnohead"))
72 device_type = DEVICE_TYPE_TUN;
73 else if(!strcasecmp(type, "tunifhead"))
74 device_type = DEVICE_TYPE_TUNIFHEAD;
75 else if(!strcasecmp(type, "tap"))
76 device_type = DEVICE_TYPE_TAP;
78 logger(LOG_ERR, _("Unknown device type %s!"), type);
82 if(strstr(device, "tap") || routing_mode != RMODE_ROUTER)
83 device_type = DEVICE_TYPE_TAP;
88 device_type = DEVICE_TYPE_TUN;
93 if(ioctl(device_fd, TUNSIFHEAD, &zero, sizeof zero) == -1) {
94 logger(LOG_ERR, _("System call `%s' failed: %s"), "ioctl", strerror(errno));
99 #if defined(TUNSIFMODE) && defined(IFF_BROADCAST) && defined(IFF_MULTICAST)
101 const int mode = IFF_BROADCAST | IFF_MULTICAST;
102 ioctl(device_fd, TUNSIFMODE, &mode, sizeof mode);
106 device_info = _("Generic BSD tun device");
108 case DEVICE_TYPE_TUNIFHEAD:
112 if(ioctl(device_fd, TUNSIFHEAD, &one, sizeof one) == -1) {
113 logger(LOG_ERR, _("System call `%s' failed: %s"), "ioctl", strerror(errno));
118 #if defined(TUNSIFMODE) && defined(IFF_BROADCAST) && defined(IFF_MULTICAST)
120 const int mode = IFF_BROADCAST | IFF_MULTICAST;
121 ioctl(device_fd, TUNSIFMODE, &mode, sizeof mode);
125 device_info = _("Generic BSD tun device");
127 case DEVICE_TYPE_TAP:
128 if(routing_mode == RMODE_ROUTER)
129 overwrite_mac = true;
130 device_info = _("Generic BSD tap device");
134 logger(LOG_INFO, _("%s is a %s"), device, device_info);
139 void close_device(void) {
148 bool read_packet(vpn_packet_t *packet) {
153 switch(device_type) {
154 case DEVICE_TYPE_TUN:
155 if((lenin = read(device_fd, packet->data + 14, MTU - 14)) <= 0) {
156 logger(LOG_ERR, _("Error while reading from %s %s: %s"), device_info,
157 device, strerror(errno));
161 switch(packet->data[14] >> 4) {
163 packet->data[12] = 0x08;
164 packet->data[13] = 0x00;
167 packet->data[12] = 0x86;
168 packet->data[13] = 0xDD;
171 ifdebug(TRAFFIC) logger(LOG_ERR,
172 _ ("Unknown IP version %d while reading packet from %s %s"),
173 packet->data[14] >> 4, device_info, device);
177 packet->len = lenin + 14;
180 case DEVICE_TYPE_TUNIFHEAD: {
182 struct iovec vector[2] = {{&type, sizeof(type)}, {packet->data + 14, MTU - 14}};
184 if((lenin = readv(device_fd, vector, 2)) <= 0) {
185 logger(LOG_ERR, _("Error while reading from %s %s: %s"), device_info,
186 device, strerror(errno));
190 switch (ntohl(type)) {
192 packet->data[12] = 0x08;
193 packet->data[13] = 0x00;
197 packet->data[12] = 0x86;
198 packet->data[13] = 0xDD;
202 ifdebug(TRAFFIC) logger(LOG_ERR,
203 _ ("Unknown address family %x while reading packet from %s %s"),
204 ntohl(type), device_info, device);
208 packet->len = lenin + 10;
212 case DEVICE_TYPE_TAP:
213 if((lenin = read(device_fd, packet->data, MTU)) <= 0) {
214 logger(LOG_ERR, _("Error while reading from %s %s: %s"), device_info,
215 device, strerror(errno));
226 device_total_in += packet->len;
228 ifdebug(TRAFFIC) logger(LOG_DEBUG, _("Read packet of %d bytes from %s"),
229 packet->len, device_info);
234 bool write_packet(vpn_packet_t *packet)
238 ifdebug(TRAFFIC) logger(LOG_DEBUG, _("Writing packet of %d bytes to %s"),
239 packet->len, device_info);
241 switch(device_type) {
242 case DEVICE_TYPE_TUN:
243 if(write(device_fd, packet->data + 14, packet->len - 14) < 0) {
244 logger(LOG_ERR, _("Error while writing to %s %s: %s"), device_info,
245 device, strerror(errno));
250 case DEVICE_TYPE_TUNIFHEAD: {
252 struct iovec vector[2] = {{&type, sizeof(type)}, {packet->data + 14, packet->len - 14}};
255 af = (packet->data[12] << 8) + packet->data[13];
259 type = htonl(AF_INET);
262 type = htonl(AF_INET6);
265 ifdebug(TRAFFIC) logger(LOG_ERR,
266 _("Unknown address family %x while writing packet to %s %s"),
267 af, device_info, device);
271 if(writev(device_fd, vector, 2) < 0) {
272 logger(LOG_ERR, _("Can't write to %s %s: %s"), device_info, device,
279 case DEVICE_TYPE_TAP:
280 if(write(device_fd, packet->data, packet->len) < 0) {
281 logger(LOG_ERR, _("Error while writing to %s %s: %s"), device_info,
282 device, strerror(errno));
291 device_total_out += packet->len;
296 void dump_device_stats(void)
300 logger(LOG_DEBUG, _("Statistics for %s %s:"), device_info, device);
301 logger(LOG_DEBUG, _(" total bytes in: %10d"), device_total_in);
302 logger(LOG_DEBUG, _(" total bytes out: %10d"), device_total_out);