2 device.c -- Interaction with Linux ethertap and tun/tap device
3 Copyright (C) 2001 Ivo Timmermans <itimmermans@bigfoot.com>,
4 2001 Guus Sliepen <guus@sliepen.warande.net>
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,v 1.1.2.3 2001/10/27 13:13:35 guus Exp $
26 #include <sys/types.h>
33 #include <sys/ioctl.h>
37 #include LINUX_IF_TUN_H
39 #include <linux/if_tun.h>
41 #define DEFAULT_DEVICE "/dev/misc/net/tun"
43 #define DEFAULT_DEVICE "/dev/tap0"
53 #define DEVICE_TYPE_ETHERTAP 0
54 #define DEVICE_TYPE_TUNTAP 1
61 int device_total_in = 0;
62 int device_total_out = 0;
67 open the local ethertap device
69 int setup_device(void)
74 if(!get_config_string(lookup_config(config_tree, "Device"), &device_fname))
75 device_fname = DEFAULT_DEVICE;
78 if((device_fd = open(device_fname, O_RDWR | O_NONBLOCK)) < 0)
80 syslog(LOG_ERR, _("Could not open %s: %m"), device_fname);
84 /* Set default MAC address for ethertap devices */
86 mymac.type = SUBNET_MAC;
87 mymac.net.mac.address.x[0] = 0xfe;
88 mymac.net.mac.address.x[1] = 0xfd;
89 mymac.net.mac.address.x[2] = 0x00;
90 mymac.net.mac.address.x[3] = 0x00;
91 mymac.net.mac.address.x[4] = 0x00;
92 mymac.net.mac.address.x[5] = 0x00;
95 /* Ok now check if this is an old ethertap or a new tun/tap thingie */
97 memset(&ifr, 0, sizeof(ifr));
99 ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
101 strncpy(ifr.ifr_name, netname, IFNAMSIZ);
103 if (!ioctl(device_fd, TUNSETIFF, (void *) &ifr))
105 device_info = _("Linux tun/tap device");
106 device_type = DEVICE_TYPE_TUNTAP;
109 if (!ioctl(device_fd, (('T'<< 8) | 202), (void *) &ifr))
111 syslog(LOG_WARNING, _("Old ioctl() request was needed for %s"), device_fname);
112 device_type = DEVICE_TYPE_TUNTAP;
113 device_info = _("Linux tun/tap device");
118 device_info = _("Linux ethertap device");
119 device_type = DEVICE_TYPE_ETHERTAP;
122 syslog(LOG_INFO, _("%s is a %s"), device_fname, device_info);
127 void close_device(void)
134 read, encrypt and send data that is
135 available through the ethertap device
137 int read_packet(vpn_packet_t *packet)
141 if(device_type == DEVICE_TYPE_TUNTAP)
143 if((lenin = read(device_fd, packet->data, MTU)) <= 0)
145 syslog(LOG_ERR, _("Error while reading from %s %s: %m"), device_info, device_fname);
153 struct iovec vector[2] = {{&packet->len, 2}, {packet->data, MTU}};
155 if((lenin = readv(device_fd, vector, 2)) <= 0)
157 syslog(LOG_ERR, _("Error while reading from %s %s: %m"), device_info, device_fname);
161 packet->len = lenin - 2;
164 device_total_in += packet->len;
166 if(debug_lvl >= DEBUG_TRAFFIC)
168 syslog(LOG_DEBUG, _("Read packet of %d bytes from %s"), packet->len, device_info);
175 int write_packet(vpn_packet_t *packet)
178 if(debug_lvl >= DEBUG_TRAFFIC)
179 syslog(LOG_DEBUG, _("Writing packet of %d bytes to %s"),
180 packet->len, device_info);
182 if(device_type == DEVICE_TYPE_TUNTAP)
184 if(write(device_fd, packet->data, packet->len) < 0)
186 syslog(LOG_ERR, _("Can't write to %s %s: %m"), device_info, device_fname);
192 struct iovec vector[2] = {{&packet->len, 2}, {packet->data, MTU}};
194 if(writev(device_fd, vector, 2) < 0)
196 syslog(LOG_ERR, _("Can't write to %s %s: %m"), device_info, device_fname);
201 device_total_out += packet->len;
206 void dump_device_stats(void)
209 syslog(LOG_DEBUG, _("Statistics for %s %s:"), device_info, device_fname);
210 syslog(LOG_DEBUG, _(" total bytes in: %10d"), device_total_in);
211 syslog(LOG_DEBUG, _(" total bytes out: %10d"), device_total_out);