2 device.c -- Interaction with OpenBSD tun 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.2 2001/10/12 15:52:03 guus Exp $
23 #define DEFAULT_DEVICE "/dev/tun0"
25 #define DEVICE_TYPE_ETHERTAP 0
26 #define DEVICE_TYPE_TUNTAP 1
33 int device_total_in = 0;
34 int device_total_out = 0;
37 open the local ethertap device
39 int setup_device(void)
41 if(!get_config_string(lookup_config(config_tree, "Device"), &device_fname)))
42 device_fname = DEFAULT_DEVICE;
45 if((device_fd = open(device_fname, O_RDWR | O_NONBLOCK)) < 0)
47 syslog(LOG_ERR, _("Could not open %s: %m"), device_fname);
51 /* Set default MAC address for ethertap devices */
53 mymac.type = SUBNET_MAC;
54 mymac.net.mac.address.x[0] = 0xfe;
55 mymac.net.mac.address.x[1] = 0xfd;
56 mymac.net.mac.address.x[2] = 0x00;
57 mymac.net.mac.address.x[3] = 0x00;
58 mymac.net.mac.address.x[4] = 0x00;
59 mymac.net.mac.address.x[5] = 0x00;
61 device_info = _("OpenBSD tun device");
63 syslog(LOG_INFO, _("%s is a %s"), device_fname, device_info);
68 int read_packet(vpn_packet_t *packet)
73 struct iovec vector[2] = {{&type, sizeof(type)}, {packet->data + 14, MTU - 14}};
75 if((lenin = readv(device_fd, vector, 2)) <= 0)
77 syslog(LOG_ERR, _("Error while reading from %s %s: %m"), device_info, device_fname);
81 memcpy(vp->data, mymac.net.mac.address.x, 6);
82 memcpy(vp->data + 6, mymac.net.mac.address.x, 6);
86 packet->len = lenin + 10;
88 device_total_in += packet->len;
90 if(debug_lvl >= DEBUG_TRAFFIC)
92 syslog(LOG_DEBUG, _("Read packet of %d bytes from %s"), device_info, packet.len);
99 int write_packet(vpn_packet_t *packet)
101 u_int32_t type = htonl(AF_INET);
103 if(debug_lvl >= DEBUG_TRAFFIC)
104 syslog(LOG_DEBUG, _("Writing packet of %d bytes to %s"),
105 packet->len, device_info);
108 struct iovec vector[2] = {{&type, sizeof(type)}, {packet->data + 14, packet->len - 14}};
110 if(writev(device_fd, vector, 2) < 0)
112 syslog(LOG_ERR, _("Can't write to %s %s: %m"), device_info, packet.len);
116 device_total_out += packet->len;