Forgot the tun specific stuff.
[oweals/tinc.git] / 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.2 2001/10/12 15:52:03 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 + 14, MTU - 14}};
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   memcpy(vp->data, mymac.net.mac.address.x, 6);
82   memcpy(vp->data + 6, mymac.net.mac.address.x, 6);
83   vp->data[12] = 0x08;
84   vp->data[13] = 0x00;
85
86   packet->len = lenin + 10;
87
88   device_total_in += packet->len;
89
90   if(debug_lvl >= DEBUG_TRAFFIC)
91     {
92       syslog(LOG_DEBUG, _("Read packet of %d bytes from %s"), device_info, packet.len);
93     }
94
95   return 0;
96 cp
97 }
98
99 int write_packet(vpn_packet_t *packet)
100 {
101   u_int32_t type = htonl(AF_INET);
102 cp
103   if(debug_lvl >= DEBUG_TRAFFIC)
104     syslog(LOG_DEBUG, _("Writing packet of %d bytes to %s"),
105            packet->len, device_info);
106
107
108   struct iovec vector[2] = {{&type, sizeof(type)}, {packet->data + 14, packet->len - 14}};
109
110   if(writev(device_fd, vector, 2) < 0)
111     {
112       syslog(LOG_ERR, _("Can't write to %s %s: %m"), device_info, packet.len);
113       return -1;
114     }
115
116   device_total_out += packet->len;
117 cp
118 }