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