Remove all occurences of $Id$.
[tinc] / src / bsd / device.c
1 /*
2     device.c -- Interaction BSD tun/tap device
3     Copyright (C) 2001-2005 Ivo Timmermans,
4                   2001-2009 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
21 #include "system.h"
22
23 #include "conf.h"
24 #include "logger.h"
25 #include "net.h"
26 #include "route.h"
27 #include "utils.h"
28 #include "xalloc.h"
29
30 #ifdef HAVE_TUNEMU
31 #include "bsd/tunemu.h"
32 #endif
33
34 #define DEFAULT_DEVICE "/dev/tun0"
35
36 typedef enum device_type {
37         DEVICE_TYPE_TUN,
38         DEVICE_TYPE_TUNIFHEAD,
39         DEVICE_TYPE_TAP,
40 #ifdef HAVE_TUNEMU
41         DEVICE_TYPE_TUNEMU,
42 #endif
43 } device_type_t;
44
45 int device_fd = -1;
46 char *device = NULL;
47 char *iface = NULL;
48 static char *device_info = NULL;
49 static int device_total_in = 0;
50 static int device_total_out = 0;
51 #if defined(TUNEMU)
52 static device_type_t device_type = DEVICE_TYPE_TUNEMU;
53 #elif defined(HAVE_OPENBSD) || defined(HAVE_FREEBSD)
54 static device_type_t device_type = DEVICE_TYPE_TUNIFHEAD;
55 #else
56 static device_type_t device_type = DEVICE_TYPE_TUN;
57 #endif
58
59 bool setup_device(void) {
60         char *type;
61
62         cp();
63
64         if(!get_config_string(lookup_config(config_tree, "Device"), &device))
65                 device = xstrdup(DEFAULT_DEVICE);
66
67         if(!get_config_string(lookup_config(config_tree, "Interface"), &iface))
68                 iface = xstrdup(rindex(device, '/') ? rindex(device, '/') + 1 : device);
69
70         if(get_config_string(lookup_config(config_tree, "DeviceType"), &type)) {
71                 if(!strcasecmp(type, "tun"))
72                         /* use default */;      
73 #ifdef HAVE_TUNEMU
74                 else if(!strcasecmp(type, "tunemu"))
75                         device_type = DEVICE_TYPE_TUNEMU;
76 #endif
77                 else if(!strcasecmp(type, "tunnohead"))
78                         device_type = DEVICE_TYPE_TUN;
79                 else if(!strcasecmp(type, "tunifhead"))
80                         device_type = DEVICE_TYPE_TUNIFHEAD;
81                 else if(!strcasecmp(type, "tap"))
82                         device_type = DEVICE_TYPE_TAP;
83                 else {
84                         logger(LOG_ERR, _("Unknown device type %s!"), type);
85                         return false;
86                 }
87         } else {
88                 if(strstr(device, "tap") || routing_mode != RMODE_ROUTER)
89                         device_type = DEVICE_TYPE_TAP;
90         }
91
92         switch(device_type) {
93 #ifdef HAVE_TUNEMU
94                 case DEVICE_TYPE_TUNEMU: {
95                         char dynamic_name[256] = "";
96                         device_fd = tunemu_open(dynamic_name);
97                 }
98                         break;
99 #endif
100                 default:
101                         device_fd = open(device, O_RDWR | O_NONBLOCK);
102         }
103
104         if(device_fd < 0) {
105                 logger(LOG_ERR, _("Could not open %s: %s"), device, strerror(errno));
106                 return false;
107         }
108
109         switch(device_type) {
110                 default:
111                         device_type = DEVICE_TYPE_TUN;
112                 case DEVICE_TYPE_TUN:
113 #ifdef TUNSIFHEAD
114                 {       
115                         const int zero = 0;
116                         if(ioctl(device_fd, TUNSIFHEAD, &zero, sizeof zero) == -1) {
117                                 logger(LOG_ERR, _("System call `%s' failed: %s"), "ioctl", strerror(errno));
118                                 return false;
119                         }
120                 }
121 #endif
122 #if defined(TUNSIFMODE) && defined(IFF_BROADCAST) && defined(IFF_MULTICAST)
123                 {
124                         const int mode = IFF_BROADCAST | IFF_MULTICAST;
125                         ioctl(device_fd, TUNSIFMODE, &mode, sizeof mode);
126                 }
127 #endif
128
129                         device_info = _("Generic BSD tun device");
130                         break;
131                 case DEVICE_TYPE_TUNIFHEAD:
132 #ifdef TUNSIFHEAD
133                 {
134                         const int one = 1;
135                         if(ioctl(device_fd, TUNSIFHEAD, &one, sizeof one) == -1) {
136                                 logger(LOG_ERR, _("System call `%s' failed: %s"), "ioctl", strerror(errno));
137                                 return false;
138                         }
139                 }
140 #endif
141 #if defined(TUNSIFMODE) && defined(IFF_BROADCAST) && defined(IFF_MULTICAST)
142                 {
143                                 const int mode = IFF_BROADCAST | IFF_MULTICAST;
144                                 ioctl(device_fd, TUNSIFMODE, &mode, sizeof mode);
145                 }
146 #endif
147
148                         device_info = _("Generic BSD tun device");
149                         break;
150                 case DEVICE_TYPE_TAP:
151                         if(routing_mode == RMODE_ROUTER)
152                                 overwrite_mac = true;
153                         device_info = _("Generic BSD tap device");
154                         break;
155 #ifdef HAVE_TUNEMU
156                 case DEVICE_TYPE_TUNEMU:
157                         device_info = _("BSD tunemu device");
158                         break;
159 #endif
160         }
161
162         logger(LOG_INFO, _("%s is a %s"), device, device_info);
163
164         return true;
165 }
166
167 void close_device(void) {
168         cp();
169
170         switch(device_type) {
171 #ifdef HAVE_TUNEMU
172                 case DEVICE_TYPE_TUNEMU:
173                         tunemu_close(device_fd);
174                         break;
175 #endif
176                 default:
177                         close(device_fd);
178         }
179
180         free(device);
181         free(iface);
182 }
183
184 bool read_packet(vpn_packet_t *packet) {
185         int lenin;
186
187         cp();
188
189         switch(device_type) {
190                 case DEVICE_TYPE_TUN:
191 #ifdef HAVE_TUNEMU
192                 case DEVICE_TYPE_TUNEMU:
193                         if(device_type == DEVICE_TYPE_TUNEMU)
194                                 lenin = tunemu_read(device_fd, packet->data + 14, MTU - 14);
195                         else
196 #else
197                                 lenin = read(device_fd, packet->data + 14, MTU - 14);
198 #endif
199
200                         if(lenin <= 0) {
201                                 logger(LOG_ERR, _("Error while reading from %s %s: %s"), device_info,
202                                            device, strerror(errno));
203                                 return false;
204                         }
205
206                         switch(packet->data[14] >> 4) {
207                                 case 4:
208                                         packet->data[12] = 0x08;
209                                         packet->data[13] = 0x00;
210                                         break;
211                                 case 6:
212                                         packet->data[12] = 0x86;
213                                         packet->data[13] = 0xDD;
214                                         break;
215                                 default:
216                                         ifdebug(TRAFFIC) logger(LOG_ERR,
217                                                            _ ("Unknown IP version %d while reading packet from %s %s"),
218                                                            packet->data[14] >> 4, device_info, device);
219                                         return false;
220                         }
221
222                         packet->len = lenin + 14;
223                         break;
224
225                 case DEVICE_TYPE_TUNIFHEAD: {
226                         u_int32_t type;
227                         struct iovec vector[2] = {{&type, sizeof(type)}, {packet->data + 14, MTU - 14}};
228
229                         if((lenin = readv(device_fd, vector, 2)) <= 0) {
230                                 logger(LOG_ERR, _("Error while reading from %s %s: %s"), device_info,
231                                            device, strerror(errno));
232                                 return false;
233                         }
234
235                         switch (ntohl(type)) {
236                                 case AF_INET:
237                                         packet->data[12] = 0x08;
238                                         packet->data[13] = 0x00;
239                                         break;
240
241                                 case AF_INET6:
242                                         packet->data[12] = 0x86;
243                                         packet->data[13] = 0xDD;
244                                         break;
245
246                                 default:
247                                         ifdebug(TRAFFIC) logger(LOG_ERR,
248                                                            _ ("Unknown address family %x while reading packet from %s %s"),
249                                                            ntohl(type), device_info, device);
250                                         return false;
251                         }
252
253                         packet->len = lenin + 10;
254                         break;
255                 }
256
257                 case DEVICE_TYPE_TAP:
258                         if((lenin = read(device_fd, packet->data, MTU)) <= 0) {
259                                 logger(LOG_ERR, _("Error while reading from %s %s: %s"), device_info,
260                                            device, strerror(errno));
261                                 return false;
262                         }
263
264                         packet->len = lenin;
265                         break;
266
267                 default:
268                         return false;
269         }
270                 
271         device_total_in += packet->len;
272
273         ifdebug(TRAFFIC) logger(LOG_DEBUG, _("Read packet of %d bytes from %s"),
274                            packet->len, device_info);
275
276         logger(LOG_INFO, "E:fd_read");
277         return true;
278 }
279
280 bool write_packet(vpn_packet_t *packet)
281 {
282         cp();
283
284         ifdebug(TRAFFIC) logger(LOG_DEBUG, _("Writing packet of %d bytes to %s"),
285                            packet->len, device_info);
286
287         switch(device_type) {
288                 case DEVICE_TYPE_TUN:
289                         if(write(device_fd, packet->data + 14, packet->len - 14) < 0) {
290                                 logger(LOG_ERR, _("Error while writing to %s %s: %s"), device_info,
291                                            device, strerror(errno));
292                                 return false;
293                         }
294                         break;
295
296                 case DEVICE_TYPE_TUNIFHEAD: {
297                         u_int32_t type;
298                         struct iovec vector[2] = {{&type, sizeof(type)}, {packet->data + 14, packet->len - 14}};
299                         int af;
300                         
301                         af = (packet->data[12] << 8) + packet->data[13];
302
303                         switch (af) {
304                                 case 0x0800:
305                                         type = htonl(AF_INET);
306                                         break;
307                                 case 0x86DD:
308                                         type = htonl(AF_INET6);
309                                         break;
310                                 default:
311                                         ifdebug(TRAFFIC) logger(LOG_ERR,
312                                                            _("Unknown address family %x while writing packet to %s %s"),
313                                                            af, device_info, device);
314                                         return false;
315                         }
316
317                         if(writev(device_fd, vector, 2) < 0) {
318                                 logger(LOG_ERR, _("Can't write to %s %s: %s"), device_info, device,
319                                            strerror(errno));
320                                 return false;
321                         }
322                         break;
323                 }
324                         
325                 case DEVICE_TYPE_TAP:
326                         if(write(device_fd, packet->data, packet->len) < 0) {
327                                 logger(LOG_ERR, _("Error while writing to %s %s: %s"), device_info,
328                                            device, strerror(errno));
329                                 return false;
330                         }
331                         break;
332
333 #ifdef HAVE_TUNEMU
334                 case DEVICE_TYPE_TUNEMU:
335                         if(tunemu_write(device_fd, packet->data + 14, packet->len - 14) < 0) {
336                                 logger(LOG_ERR, _("Error while writing to %s %s: %s"), device_info,
337                                            device, strerror(errno));
338                                 return false;
339                         }
340                         break;
341 #endif
342
343                 default:
344                         return false;
345         }
346
347         device_total_out += packet->len;
348
349         return true;
350 }
351
352 void dump_device_stats(void)
353 {
354         cp();
355
356         logger(LOG_DEBUG, _("Statistics for %s %s:"), device_info, device);
357         logger(LOG_DEBUG, _(" total bytes in:  %10d"), device_total_in);
358         logger(LOG_DEBUG, _(" total bytes out: %10d"), device_total_out);
359 }