2 device.c -- Interaction with FreeBSD 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.1 2001/10/12 15:22:59 guus Exp $
23 #define DEFAULT_DEVICE "/dev/tap0"
30 int device_total_in = 0;
31 int device_total_out = 0;
34 open the local ethertap device
36 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 device_fd = device_fd;
53 /* Set default MAC address for ethertap devices */
55 mymac.type = SUBNET_MAC;
56 mymac.net.mac.address.x[0] = 0xfe;
57 mymac.net.mac.address.x[1] = 0xfd;
58 mymac.net.mac.address.x[2] = 0x00;
59 mymac.net.mac.address.x[3] = 0x00;
60 mymac.net.mac.address.x[4] = 0x00;
61 mymac.net.mac.address.x[5] = 0x00;
63 device_info = _("FreeBSD tap device");
65 syslog(LOG_INFO, _("%s is a %s"), device_fname, device_info);
71 read, encrypt and send data that is
72 available through the ethertap device
74 int read_packet(vpn_packet_t *packet)
78 if((lenin = read(device_fd, packet->data, MTU)) <= 0)
80 syslog(LOG_ERR, _("Error while reading from %s %s: %m"), device_info, device_fname);
86 device_total_in += packet->len;
88 if(debug_lvl >= DEBUG_TRAFFIC)
89 syslog(LOG_DEBUG, _("Read packet of %d bytes from %s"),
90 packet->len, device_info);
96 int write_packet(vpn_packet_t *packet)
99 if(debug_lvl >= DEBUG_TRAFFIC)
100 syslog(LOG_DEBUG, _("Writing packet of %d bytes to %s"),
101 packet->len, device_info);
103 if(write(device_fd, packet->data, packet->len) < 0)
105 syslog(LOG_ERR, _("Error while writing to %s %s: %m"), device_info, device_fname);
109 device_total_out += packet->len;