045fc188241b03f07864453cba54f760bb758cf8
[oweals/tinc.git] / src / bsd / device.c
1 /*
2     device.c -- Interaction BSD tun/tap device
3     Copyright (C) 2001-2004 Ivo Timmermans <ivo@tinc-vpn.org>,
4                   2001-2004 Guus Sliepen <guus@tinc-vpn.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 1398 2004-11-01 15:18:53Z guus $
21 */
22
23 #include "system.h"
24
25 #include "conf.h"
26 #include "logger.h"
27 #include "net.h"
28 #include "route.h"
29 #include "utils.h"
30
31 #define DEFAULT_DEVICE "/dev/tun0"
32
33 typedef enum device_type {
34         DEVICE_TYPE_TUN,
35         DEVICE_TYPE_TUNIFHEAD,
36         DEVICE_TYPE_TAP,
37 } device_type_t;
38
39 int device_fd = -1;
40 char *device;
41 char *iface;
42 char *device_info;
43 static int device_total_in = 0;
44 static int device_total_out = 0;
45 #ifdef HAVE_OPENBSD
46 static device_type_t device_type = DEVICE_TYPE_TUNIFHEAD;
47 #else
48 static device_type_t device_type = DEVICE_TYPE_TUN;
49 #endif
50
51 bool setup_device(void) {
52 #ifdef TUNSIFHEAD
53         const int zero = 0;
54         const int one = 1;
55 #endif
56         char *type;
57
58         cp();
59
60         if(!get_config_string(lookup_config(config_tree, "Device"), &device))
61                 device = DEFAULT_DEVICE;
62
63         if(!get_config_string(lookup_config(config_tree, "Interface"), &iface))
64                 iface = rindex(device, '/') ? rindex(device, '/') + 1 : device;
65
66         if((device_fd = open(device, O_RDWR | O_NONBLOCK)) < 0) {
67                 logger(LOG_ERR, _("Could not open %s: %s"), device, strerror(errno));
68                 return false;
69         }
70
71         if(get_config_string(lookup_config(config_tree, "DeviceType"), &type)) {
72                 if(!strcasecmp(type, "tun"))
73                         /* use default */;      
74                 else if(!strcasecmp(type, "tunnohead"))
75                         device_type = DEVICE_TYPE_TUN;
76                 else if(!strcasecmp(type, "tunifhead"))
77                         device_type = DEVICE_TYPE_TUNIFHEAD;
78                 else if(!strcasecmp(type, "tap"))
79                         device_type = DEVICE_TYPE_TAP;
80                 else {
81                         logger(LOG_ERR, _("Unknown device type %s!"), type);
82                         return false;
83                 }
84         } else {
85                 if(strstr(device, "tap"))
86                         device_type = DEVICE_TYPE_TAP;
87         }
88
89         switch(device_type) {
90                 default:
91                         device_type = DEVICE_TYPE_TUN;
92                 case DEVICE_TYPE_TUN:
93 #ifdef TUNSIFHEAD
94                         if(ioctl(device_fd, TUNSIFHEAD, &zero, sizeof zero) == -1) {
95                                 logger(LOG_ERR, _("System call `%s' failed: %s"), "ioctl", strerror(errno));
96                                 return false;
97                         }
98 #endif
99                         device_info = _("Generic BSD tun device");
100                         break;
101                 case DEVICE_TYPE_TUNIFHEAD:
102 #ifdef TUNSIFHEAD
103                         if(ioctl(device_fd, TUNSIFHEAD, &one, sizeof one) == -1) {
104                                 logger(LOG_ERR, _("System call `%s' failed: %s"), "ioctl", strerror(errno));
105                                 return false;
106                         }
107 #endif
108                         device_info = _("Generic BSD tun device");
109                         break;
110                 case DEVICE_TYPE_TAP:
111                         if(routing_mode == RMODE_ROUTER)
112                                 overwrite_mac = true;
113                         device_info = _("Generic BSD tap device");
114                         break;
115         }
116
117         logger(LOG_INFO, _("%s is a %s"), device, device_info);
118
119         return true;
120 }
121
122 void close_device(void) {
123         cp();
124
125         close(device_fd);
126 }
127
128 bool read_packet(vpn_packet_t *packet) {
129         int lenin;
130
131         cp();
132
133         switch(device_type) {
134                 case DEVICE_TYPE_TUN:
135                         if((lenin = read(device_fd, packet->data + 14, MTU - 14)) <= 0) {
136                                 logger(LOG_ERR, _("Error while reading from %s %s: %s"), device_info,
137                                            device, strerror(errno));
138                                 return false;
139                         }
140
141                         switch(packet->data[14] >> 4) {
142                                 case 4:
143                                         packet->data[12] = 0x08;
144                                         packet->data[13] = 0x00;
145                                         break;
146                                 case 6:
147                                         packet->data[12] = 0x86;
148                                         packet->data[13] = 0xDD;
149                                         break;
150                                 default:
151                                         ifdebug(TRAFFIC) logger(LOG_ERR,
152                                                            _ ("Unknown IP version %d while reading packet from %s %s"),
153                                                            packet->data[14] >> 4, device_info, device);
154                                         return false;
155                         }
156
157                         packet->len = lenin + 14;
158                         break;
159
160                 case DEVICE_TYPE_TUNIFHEAD: {
161                         u_int32_t type;
162                         struct iovec vector[2] = {{&type, sizeof(type)}, {packet->data + 14, MTU - 14}};
163
164                         if((lenin = readv(device_fd, vector, 2)) <= 0) {
165                                 logger(LOG_ERR, _("Error while reading from %s %s: %s"), device_info,
166                                            device, strerror(errno));
167                                 return false;
168                         }
169
170                         switch (ntohl(type)) {
171                                 case AF_INET:
172                                         packet->data[12] = 0x08;
173                                         packet->data[13] = 0x00;
174                                         break;
175
176                                 case AF_INET6:
177                                         packet->data[12] = 0x86;
178                                         packet->data[13] = 0xDD;
179                                         break;
180
181                                 default:
182                                         ifdebug(TRAFFIC) logger(LOG_ERR,
183                                                            _ ("Unknown address family %x while reading packet from %s %s"),
184                                                            ntohl(type), device_info, device);
185                                         return false;
186                         }
187
188                         packet->len = lenin + 10;
189                         break;
190                 }
191
192                 case DEVICE_TYPE_TAP:
193                         if((lenin = read(device_fd, packet->data, MTU)) <= 0) {
194                                 logger(LOG_ERR, _("Error while reading from %s %s: %s"), device_info,
195                                            device, strerror(errno));
196                                 return false;
197                         }
198
199                         packet->len = lenin;
200                         break;
201
202                 default:
203                         return false;
204         }
205                 
206         device_total_in += packet->len;
207
208         ifdebug(TRAFFIC) logger(LOG_DEBUG, _("Read packet of %d bytes from %s"),
209                            packet->len, device_info);
210
211         return true;
212 }
213
214 bool write_packet(vpn_packet_t *packet)
215 {
216         cp();
217
218         ifdebug(TRAFFIC) logger(LOG_DEBUG, _("Writing packet of %d bytes to %s"),
219                            packet->len, device_info);
220
221         switch(device_type) {
222                 case DEVICE_TYPE_TUN:
223                         if(write(device_fd, packet->data + 14, packet->len - 14) < 0) {
224                                 logger(LOG_ERR, _("Error while writing to %s %s: %s"), device_info,
225                                            device, strerror(errno));
226                                 return false;
227                         }
228                         break;
229
230                 case DEVICE_TYPE_TUNIFHEAD: {
231                         u_int32_t type;
232                         struct iovec vector[2] = {{&type, sizeof(type)}, {packet->data + 14, MTU - 14}};
233                         int af;
234                         
235                         af = (packet->data[12] << 8) + packet->data[13];
236
237                         switch (af) {
238                                 case 0x0800:
239                                         type = htonl(AF_INET);
240                                         break;
241                                 case 0x86DD:
242                                         type = htonl(AF_INET6);
243                                         break;
244                                 default:
245                                         ifdebug(TRAFFIC) logger(LOG_ERR,
246                                                            _("Unknown address family %x while writing packet to %s %s"),
247                                                            af, device_info, device);
248                                         return false;
249                         }
250
251                         if(writev(device_fd, vector, 2) < 0) {
252                                 logger(LOG_ERR, _("Can't write to %s %s: %s"), device_info, device,
253                                            strerror(errno));
254                                 return false;
255                         }
256                         break;
257                 }
258                         
259                 case DEVICE_TYPE_TAP:
260                         if(write(device_fd, packet->data, packet->len) < 0) {
261                                 logger(LOG_ERR, _("Error while writing to %s %s: %s"), device_info,
262                                            device, strerror(errno));
263                                 return false;
264                         }
265                         break;
266
267                 default:
268                         return false;
269         }
270
271         device_total_out += packet->len;
272
273         return true;
274 }
275
276 void dump_device_stats(void)
277 {
278         cp();
279
280         logger(LOG_DEBUG, _("Statistics for %s %s:"), device_info, device);
281         logger(LOG_DEBUG, _(" total bytes in:  %10d"), device_total_in);
282         logger(LOG_DEBUG, _(" total bytes out: %10d"), device_total_out);
283 }