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