ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/gvpe/src/tincd/openbsd/device.c
Revision: 1.1
Committed: Tue Oct 14 03:22:09 2003 UTC (20 years, 8 months ago) by pcg
Content type: text/plain
Branch: MAIN
CVS Tags: rel-1_9, rel-1_8, rel-2_01, poll-based-iom, rel-3_0, VPE_1_2, rel-2_2, rel-2_0, VPE_1_4, VPE_1_6, rel-1_7, VPE-1_6_1, rel-2_21, rel-2_22, rel-2_25, HEAD
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 pcg 1.1 /*
2     device.c -- Interaction with OpenBSD tun device
3     Copyright (C) 2001-2003 Ivo Timmermans <ivo@o2w.nl>,
4     2001-2003 Guus Sliepen <guus@sliepen.eu.org>
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.19 2003/08/08 11:45:37 guus Exp $
21     */
22    
23     #include <sys/uio.h>
24    
25    
26     #define DEFAULT_DEVICE "/dev/tun0"
27    
28     #define DEVICE_TYPE_ETHERTAP 0
29     #define DEVICE_TYPE_TUNTAP 1
30    
31     int device_fd = -1;
32     char *device;
33     char *iface;
34     char *device_info;
35    
36     int device_total_in = 0;
37     int device_total_out = 0;
38    
39     bool setup_device(void)
40     {
41     cp();
42    
43     if(!get_config_string(lookup_config(config_tree, "Device"), &device))
44     device = DEFAULT_DEVICE;
45    
46     if(!get_config_string(lookup_config(config_tree, "Interface"), &iface))
47     iface = rindex(device, '/') ? rindex(device, '/') + 1 : device;
48     if((device_fd = open(device, O_RDWR | O_NONBLOCK)) < 0) {
49     logger(LOG_ERR, _("Could not open %s: %s"), device, strerror(errno));
50     return false;
51     }
52    
53     device_info = _("OpenBSD tun device");
54    
55     logger(LOG_INFO, _("%s is a %s"), device, device_info);
56    
57     return true;
58     }
59    
60     void close_device(void)
61     {
62     cp();
63    
64     close(device_fd);
65     }
66    
67     bool read_packet(vpn_packet_t *packet)
68     {
69     int lenin;
70     u_int32_t type;
71     struct iovec vector[2] = {{&type, sizeof(type)}, {packet->data + 14, MTU - 14}};
72    
73     cp();
74    
75     if((lenin = readv(device_fd, vector, 2)) <= 0) {
76     logger(LOG_ERR, _("Error while reading from %s %s: %s"), device_info,
77     device, strerror(errno));
78     return false;
79     }
80    
81     switch (ntohl(type)) {
82     case AF_INET:
83     packet->data[12] = 0x8;
84     packet->data[13] = 0x0;
85     break;
86    
87     case AF_INET6:
88     packet->data[12] = 0x86;
89     packet->data[13] = 0xDD;
90     break;
91    
92     default:
93     ifdebug(TRAFFIC) logger(LOG_ERR,
94     _ ("Unknown address family %d while reading packet from %s %s"),
95     ntohl(type), device_info, device);
96     return false;
97     }
98    
99     packet->len = lenin + 10;
100    
101     device_total_in += packet->len;
102    
103     ifdebug(TRAFFIC) logger(LOG_DEBUG, _("Read packet of %d bytes from %s"), packet->len,
104     device_info);
105    
106     return true;
107     }
108    
109     bool write_packet(vpn_packet_t *packet)
110     {
111     u_int32_t type;
112     struct iovec vector[2];
113     int af;
114    
115     cp();
116    
117     ifdebug(TRAFFIC) logger(LOG_DEBUG, _("Writing packet of %d bytes to %s"),
118     packet->len, device_info);
119    
120     af = (packet->data[12] << 8) + packet->data[13];
121    
122     switch (af) {
123     case 0x800:
124     type = htonl(AF_INET);
125     break;
126     case 0x86DD:
127     type = htonl(AF_INET6);
128     break;
129     default:
130     ifdebug(TRAFFIC) logger(LOG_ERR,
131     _("Unknown address family %d while writing packet to %s %s"),
132     af, device_info, device);
133     return false;
134     }
135    
136     vector[0].iov_base = &type;
137     vector[0].iov_len = sizeof(type);
138     vector[1].iov_base = packet->data + 14;
139     vector[1].iov_len = packet->len - 14;
140    
141     if(writev(device_fd, vector, 2) < 0) {
142     logger(LOG_ERR, _("Can't write to %s %s: %s"), device_info, device,
143     strerror(errno));
144     return false;
145     }
146    
147     device_total_out += packet->len;
148    
149     return true;
150     }
151    
152     void dump_device_stats(void)
153     {
154     cp();
155    
156     logger(LOG_DEBUG, _("Statistics for %s %s:"), device_info, device);
157     logger(LOG_DEBUG, _(" total bytes in: %10d"), device_total_in);
158     logger(LOG_DEBUG, _(" total bytes out: %10d"), device_total_out);
159     }