Merge branch 'master' of black:tinc
[tinc] / src / bsd / device.c
1 /*
2     device.c -- Interaction BSD tun/tap device
3     Copyright (C) 2001-2005 Ivo Timmermans,
4                   2001-2012 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 #ifdef FD_CLOEXEC
110         fcntl(device_fd, F_SETFD, FD_CLOEXEC);
111 #endif
112
113         switch(device_type) {
114                 default:
115                         device_type = DEVICE_TYPE_TUN;
116                 case DEVICE_TYPE_TUN:
117 #ifdef TUNSIFHEAD
118                 {       
119                         const int zero = 0;
120                         if(ioctl(device_fd, TUNSIFHEAD, &zero, sizeof zero) == -1) {
121                                 logger(LOG_ERR, "System call `%s' failed: %s", "ioctl", strerror(errno));
122                                 return false;
123                         }
124                 }
125 #endif
126 #if defined(TUNSIFMODE) && defined(IFF_BROADCAST) && defined(IFF_MULTICAST)
127                 {
128                         const int mode = IFF_BROADCAST | IFF_MULTICAST;
129                         ioctl(device_fd, TUNSIFMODE, &mode, sizeof mode);
130                 }
131 #endif
132
133                         device_info = "Generic BSD tun device";
134                         break;
135                 case DEVICE_TYPE_TUNIFHEAD:
136 #ifdef TUNSIFHEAD
137                 {
138                         const int one = 1;
139                         if(ioctl(device_fd, TUNSIFHEAD, &one, sizeof one) == -1) {
140                                 logger(LOG_ERR, "System call `%s' failed: %s", "ioctl", strerror(errno));
141                                 return false;
142                         }
143                 }
144 #endif
145 #if defined(TUNSIFMODE) && defined(IFF_BROADCAST) && defined(IFF_MULTICAST)
146                 {
147                                 const int mode = IFF_BROADCAST | IFF_MULTICAST;
148                                 ioctl(device_fd, TUNSIFMODE, &mode, sizeof mode);
149                 }
150 #endif
151
152                         device_info = "Generic BSD tun device";
153                         break;
154                 case DEVICE_TYPE_TAP:
155                         if(routing_mode == RMODE_ROUTER)
156                                 overwrite_mac = true;
157                         device_info = "Generic BSD tap device";
158 #ifdef TAPGIFNAME
159                         {
160                                 struct ifreq ifr;
161                                 if(ioctl(device_fd, TAPGIFNAME, (void*)&ifr) == 0) {
162                                         if(iface)
163                                                 free(iface);
164                                         iface = xstrdup(ifr.ifr_name);
165                                 }
166                         }
167                         
168 #endif
169                         break;
170 #ifdef HAVE_TUNEMU
171                 case DEVICE_TYPE_TUNEMU:
172                         device_info = "BSD tunemu device";
173                         break;
174 #endif
175         }
176
177         logger(LOG_INFO, "%s is a %s", device, device_info);
178
179         return true;
180 }
181
182 static void close_device(void) {
183         switch(device_type) {
184 #ifdef HAVE_TUNEMU
185                 case DEVICE_TYPE_TUNEMU:
186                         tunemu_close(device_fd);
187                         break;
188 #endif
189                 default:
190                         close(device_fd);
191         }
192
193         free(device);
194         free(iface);
195 }
196
197 static bool read_packet(vpn_packet_t *packet) {
198         int lenin;
199
200         switch(device_type) {
201                 case DEVICE_TYPE_TUN:
202 #ifdef HAVE_TUNEMU
203                 case DEVICE_TYPE_TUNEMU:
204                         if(device_type == DEVICE_TYPE_TUNEMU)
205                                 lenin = tunemu_read(device_fd, packet->data + 14, MTU - 14);
206                         else
207 #endif
208                                 lenin = read(device_fd, packet->data + 14, MTU - 14);
209
210                         if(lenin <= 0) {
211                                 logger(LOG_ERR, "Error while reading from %s %s: %s", device_info,
212                                            device, strerror(errno));
213                                 return false;
214                         }
215
216                         switch(packet->data[14] >> 4) {
217                                 case 4:
218                                         packet->data[12] = 0x08;
219                                         packet->data[13] = 0x00;
220                                         break;
221                                 case 6:
222                                         packet->data[12] = 0x86;
223                                         packet->data[13] = 0xDD;
224                                         break;
225                                 default:
226                                         ifdebug(TRAFFIC) logger(LOG_ERR,
227                                                            "Unknown IP version %d while reading packet from %s %s",
228                                                            packet->data[14] >> 4, device_info, device);
229                                         return false;
230                         }
231
232                         packet->len = lenin + 14;
233                         break;
234
235                 case DEVICE_TYPE_TUNIFHEAD: {
236                         u_int32_t type;
237                         struct iovec vector[2] = {{&type, sizeof(type)}, {packet->data + 14, MTU - 14}};
238
239                         if((lenin = readv(device_fd, vector, 2)) <= 0) {
240                                 logger(LOG_ERR, "Error while reading from %s %s: %s", device_info,
241                                            device, strerror(errno));
242                                 return false;
243                         }
244
245                         switch (ntohl(type)) {
246                                 case AF_INET:
247                                         packet->data[12] = 0x08;
248                                         packet->data[13] = 0x00;
249                                         break;
250
251                                 case AF_INET6:
252                                         packet->data[12] = 0x86;
253                                         packet->data[13] = 0xDD;
254                                         break;
255
256                                 default:
257                                         ifdebug(TRAFFIC) logger(LOG_ERR,
258                                                            "Unknown address family %x while reading packet from %s %s",
259                                                            ntohl(type), device_info, device);
260                                         return false;
261                         }
262
263                         packet->len = lenin + 10;
264                         break;
265                 }
266
267                 case DEVICE_TYPE_TAP:
268                         if((lenin = read(device_fd, packet->data, MTU)) <= 0) {
269                                 logger(LOG_ERR, "Error while reading from %s %s: %s", device_info,
270                                            device, strerror(errno));
271                                 return false;
272                         }
273
274                         packet->len = lenin;
275                         break;
276
277                 default:
278                         return false;
279         }
280                 
281         device_total_in += packet->len;
282
283         ifdebug(TRAFFIC) logger(LOG_DEBUG, "Read packet of %d bytes from %s",
284                            packet->len, device_info);
285
286         return true;
287 }
288
289 static bool write_packet(vpn_packet_t *packet) {
290         ifdebug(TRAFFIC) logger(LOG_DEBUG, "Writing packet of %d bytes to %s",
291                            packet->len, device_info);
292
293         switch(device_type) {
294                 case DEVICE_TYPE_TUN:
295                         if(write(device_fd, packet->data + 14, packet->len - 14) < 0) {
296                                 logger(LOG_ERR, "Error while writing to %s %s: %s", device_info,
297                                            device, strerror(errno));
298                                 return false;
299                         }
300                         break;
301
302                 case DEVICE_TYPE_TUNIFHEAD: {
303                         u_int32_t type;
304                         struct iovec vector[2] = {{&type, sizeof(type)}, {packet->data + 14, packet->len - 14}};
305                         int af;
306                         
307                         af = (packet->data[12] << 8) + packet->data[13];
308
309                         switch (af) {
310                                 case 0x0800:
311                                         type = htonl(AF_INET);
312                                         break;
313                                 case 0x86DD:
314                                         type = htonl(AF_INET6);
315                                         break;
316                                 default:
317                                         ifdebug(TRAFFIC) logger(LOG_ERR,
318                                                            "Unknown address family %x while writing packet to %s %s",
319                                                            af, device_info, device);
320                                         return false;
321                         }
322
323                         if(writev(device_fd, vector, 2) < 0) {
324                                 logger(LOG_ERR, "Can't write to %s %s: %s", device_info, device,
325                                            strerror(errno));
326                                 return false;
327                         }
328                         break;
329                 }
330                         
331                 case DEVICE_TYPE_TAP:
332                         if(write(device_fd, packet->data, packet->len) < 0) {
333                                 logger(LOG_ERR, "Error while writing to %s %s: %s", device_info,
334                                            device, strerror(errno));
335                                 return false;
336                         }
337                         break;
338
339 #ifdef HAVE_TUNEMU
340                 case DEVICE_TYPE_TUNEMU:
341                         if(tunemu_write(device_fd, packet->data + 14, packet->len - 14) < 0) {
342                                 logger(LOG_ERR, "Error while writing to %s %s: %s", device_info,
343                                            device, strerror(errno));
344                                 return false;
345                         }
346                         break;
347 #endif
348
349                 default:
350                         return false;
351         }
352
353         device_total_out += packet->len;
354
355         return true;
356 }
357
358 static void dump_device_stats(void) {
359         logger(LOG_DEBUG, "Statistics for %s %s:", device_info, device);
360         logger(LOG_DEBUG, " total bytes in:  %10"PRIu64, device_total_in);
361         logger(LOG_DEBUG, " total bytes out: %10"PRIu64, device_total_out);
362 }
363
364 const devops_t os_devops = {
365         .setup = setup_device,
366         .close = close_device,
367         .read = read_packet,
368         .write = write_packet,
369         .dump_stats = dump_device_stats,
370 };