Allow linking with multiple device drivers.
[oweals/tinc.git] / src / bsd / device.c
1 /*
2     device.c -- Interaction BSD tun/tap device
3     Copyright (C) 2001-2005 Ivo Timmermans,
4                   2001-2011 Guus Sliepen <guus@tinc-vpn.org>
5                   2009      Grzegorz Dymarek <gregd72002@googlemail.com>
6
7     This program is free software; you can redistribute it and/or modify
8     it under the terms of the GNU General Public License as published by
9     the Free Software Foundation; either version 2 of the License, or
10     (at your option) any later version.
11
12     This program is distributed in the hope that it will be useful,
13     but WITHOUT ANY WARRANTY; without even the implied warranty of
14     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15     GNU General Public License for more details.
16
17     You should have received a copy of the GNU General Public License along
18     with this program; if not, write to the Free Software Foundation, Inc.,
19     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 */
21
22 #include "system.h"
23
24 #include "conf.h"
25 #include "device.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 uint64_t device_total_in = 0;
52 static uint64_t 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) || defined(HAVE_DRAGONFLY)
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 static bool setup_device(void) {
62         char *type;
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(strrchr(device, '/') ? strrchr(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 #ifdef TAPGIFNAME
155                         {
156                                 struct ifreq ifr;
157                                 if(ioctl(device_fd, TAPGIFNAME, (void*)&ifr) == 0) {
158                                         if(iface)
159                                                 free(iface);
160                                         iface = xstrdup(ifr.ifr_name);
161                                 }
162                         }
163                         
164 #endif
165                         break;
166 #ifdef HAVE_TUNEMU
167                 case DEVICE_TYPE_TUNEMU:
168                         device_info = "BSD tunemu device";
169                         break;
170 #endif
171         }
172
173         logger(LOG_INFO, "%s is a %s", device, device_info);
174
175         return true;
176 }
177
178 static void close_device(void) {
179         switch(device_type) {
180 #ifdef HAVE_TUNEMU
181                 case DEVICE_TYPE_TUNEMU:
182                         tunemu_close(device_fd);
183                         break;
184 #endif
185                 default:
186                         close(device_fd);
187         }
188
189         free(device);
190         free(iface);
191 }
192
193 static bool read_packet(vpn_packet_t *packet) {
194         int lenin;
195
196         switch(device_type) {
197                 case DEVICE_TYPE_TUN:
198 #ifdef HAVE_TUNEMU
199                 case DEVICE_TYPE_TUNEMU:
200                         if(device_type == DEVICE_TYPE_TUNEMU)
201                                 lenin = tunemu_read(device_fd, packet->data + 14, MTU - 14);
202                         else
203 #endif
204                                 lenin = read(device_fd, packet->data + 14, MTU - 14);
205
206                         if(lenin <= 0) {
207                                 logger(LOG_ERR, "Error while reading from %s %s: %s", device_info,
208                                            device, strerror(errno));
209                                 return false;
210                         }
211
212                         switch(packet->data[14] >> 4) {
213                                 case 4:
214                                         packet->data[12] = 0x08;
215                                         packet->data[13] = 0x00;
216                                         break;
217                                 case 6:
218                                         packet->data[12] = 0x86;
219                                         packet->data[13] = 0xDD;
220                                         break;
221                                 default:
222                                         ifdebug(TRAFFIC) logger(LOG_ERR,
223                                                            "Unknown IP version %d while reading packet from %s %s",
224                                                            packet->data[14] >> 4, device_info, device);
225                                         return false;
226                         }
227
228                         packet->len = lenin + 14;
229                         break;
230
231                 case DEVICE_TYPE_TUNIFHEAD: {
232                         u_int32_t type;
233                         struct iovec vector[2] = {{&type, sizeof(type)}, {packet->data + 14, MTU - 14}};
234
235                         if((lenin = readv(device_fd, vector, 2)) <= 0) {
236                                 logger(LOG_ERR, "Error while reading from %s %s: %s", device_info,
237                                            device, strerror(errno));
238                                 return false;
239                         }
240
241                         switch (ntohl(type)) {
242                                 case AF_INET:
243                                         packet->data[12] = 0x08;
244                                         packet->data[13] = 0x00;
245                                         break;
246
247                                 case AF_INET6:
248                                         packet->data[12] = 0x86;
249                                         packet->data[13] = 0xDD;
250                                         break;
251
252                                 default:
253                                         ifdebug(TRAFFIC) logger(LOG_ERR,
254                                                            "Unknown address family %x while reading packet from %s %s",
255                                                            ntohl(type), device_info, device);
256                                         return false;
257                         }
258
259                         packet->len = lenin + 10;
260                         break;
261                 }
262
263                 case DEVICE_TYPE_TAP:
264                         if((lenin = read(device_fd, packet->data, MTU)) <= 0) {
265                                 logger(LOG_ERR, "Error while reading from %s %s: %s", device_info,
266                                            device, strerror(errno));
267                                 return false;
268                         }
269
270                         packet->len = lenin;
271                         break;
272
273                 default:
274                         return false;
275         }
276                 
277         device_total_in += packet->len;
278
279         ifdebug(TRAFFIC) logger(LOG_DEBUG, "Read packet of %d bytes from %s",
280                            packet->len, device_info);
281
282         return true;
283 }
284
285 static bool write_packet(vpn_packet_t *packet) {
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 static void dump_device_stats(void) {
355         logger(LOG_DEBUG, "Statistics for %s %s:", device_info, device);
356         logger(LOG_DEBUG, " total bytes in:  %10"PRIu64, device_total_in);
357         logger(LOG_DEBUG, " total bytes out: %10"PRIu64, device_total_out);
358 }
359
360 const devops_t os_devops = {
361         .setup = setup_device,
362         .close = close_device,
363         .read = read_packet,
364         .write = write_packet,
365         .dump_stats = dump_device_stats,
366 };