badb459b623867c22afeefb3ef2e5dbe377a34a2
[tinc] / src / openbsd / device.c
1 /*
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>
5
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.
10
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.
15
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.
19
20     $Id: device.c,v 1.1.2.1 2001/10/12 15:49:11 guus Exp $
21 */
22
23 #define DEFAULT_DEVICE "/dev/tun0"
24
25 #define DEVICE_TYPE_ETHERTAP 0
26 #define DEVICE_TYPE_TUNTAP 1
27
28 int device_fd = -1;
29 int device_type;
30 char *device_fname;
31 char *device_info;
32
33 int device_total_in = 0;
34 int device_total_out = 0;
35
36 /*
37   open the local ethertap device
38 */
39 int setup_device(void)
40 {
41   if(!get_config_string(lookup_config(config_tree, "Device"), &device_fname)))
42     device_fname = DEFAULT_DEVICE;
43
44 cp
45   if((device_fd = open(device_fname, O_RDWR | O_NONBLOCK)) < 0)
46     {
47       syslog(LOG_ERR, _("Could not open %s: %m"), device_fname);
48       return -1;
49     }
50 cp
51   /* Set default MAC address for ethertap devices */
52
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;
60
61   device_info = _("OpenBSD tun device");
62
63   syslog(LOG_INFO, _("%s is a %s"), device_fname, device_info);
64 cp
65   return 0;
66 }
67
68 int read_packet(vpn_packet_t *packet)
69 {
70   int lenin;
71   u_int32_t type;
72 cp
73   struct iovec vector[2] = {{&type, sizeof(type)}, {packet->data, MTU}};
74
75   if((lenin = readv(device_fd, vector, 2)) <= 0)
76     {
77       syslog(LOG_ERR, _("Error while reading from %s %s: %m"), device_info, device_fname);
78       return -1;
79     }
80
81   packet->len = lenin - sizeof(type);
82
83   device_total_in += packet->len;
84
85   if(debug_lvl >= DEBUG_TRAFFIC)
86     {
87       syslog(LOG_DEBUG, _("Read packet of %d bytes from %s"), device_info, packet.len);
88     }
89
90   return 0;
91 cp
92 }
93
94 int write_packet(vpn_packet_t *packet)
95 {
96   u_int32_t type = htonl(AF_INET);
97 cp
98   if(debug_lvl >= DEBUG_TRAFFIC)
99     syslog(LOG_DEBUG, _("Writing packet of %d bytes to %s"),
100            packet->len, device_info);
101
102
103   struct iovec vector[2] = {{&type, sizeof(type)}, {packet->data, MTU}};
104
105   if(writev(device_fd, vector, 2) < 0)
106     {
107       syslog(LOG_ERR, _("Can't write to %s %s: %m"), device_info, packet.len);
108       return -1;
109     }
110
111   device_total_out += packet->len;
112 cp
113 }