6d68bc781aa7a66c71414e41cb63259e36099f4b
[tinc] / src / bsd / device.c
1 /*
2     device.c -- Interaction BSD tun/tap device
3     Copyright (C) 2001-2005 Ivo Timmermans,
4                   2001-2017 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 #ifdef HAVE_NET_IF_UTUN_H
38 #include <sys/sys_domain.h>
39 #include <sys/kern_control.h>
40 #include <net/if_utun.h>
41 #endif
42
43 #define DEFAULT_TUN_DEVICE "/dev/tun0"
44 #define DEFAULT_TAP_DEVICE "/dev/tap0"
45
46 typedef enum device_type {
47         DEVICE_TYPE_TUN,
48         DEVICE_TYPE_TUNIFHEAD,
49         DEVICE_TYPE_TAP,
50 #ifdef ENABLE_TUNEMU
51         DEVICE_TYPE_TUNEMU,
52 #endif
53         DEVICE_TYPE_UTUN,
54 } device_type_t;
55
56 int device_fd = -1;
57 char *device = NULL;
58 char *iface = NULL;
59 static char *device_info = NULL;
60 #if defined(ENABLE_TUNEMU)
61 static device_type_t device_type = DEVICE_TYPE_TUNEMU;
62 #elif defined(HAVE_OPENBSD) || defined(HAVE_FREEBSD) || defined(HAVE_DRAGONFLY)
63 static device_type_t device_type = DEVICE_TYPE_TUNIFHEAD;
64 #else
65 static device_type_t device_type = DEVICE_TYPE_TUN;
66 #endif
67
68 #ifdef HAVE_NET_IF_UTUN_H
69 static bool setup_utun(void) {
70         device_fd = socket(PF_SYSTEM, SOCK_DGRAM, SYSPROTO_CONTROL);
71
72         if(device_fd == -1) {
73                 logger(DEBUG_ALWAYS, LOG_ERR, "Could not open PF_SYSTEM socket: %s\n", strerror(errno));
74                 return false;
75         }
76
77         struct ctl_info info = {};
78
79         strlcpy(info.ctl_name, UTUN_CONTROL_NAME, sizeof(info.ctl_name));
80
81         if(ioctl(device_fd, CTLIOCGINFO, &info) == -1) {
82                 logger(DEBUG_ALWAYS, LOG_ERR, "ioctl(CTLIOCGINFO) failed: %s", strerror(errno));
83                 return false;
84         }
85
86         int unit = -1;
87         char *p = strstr(device, "utun"), *e = NULL;
88
89         if(p) {
90                 unit = strtol(p + 4, &e, 10);
91
92                 if(!e) {
93                         unit = -1;
94                 }
95         }
96
97         struct sockaddr_ctl sc = {
98                 .sc_id = info.ctl_id,
99                 .sc_len = sizeof(sc),
100                 .sc_family = AF_SYSTEM,
101                 .ss_sysaddr = AF_SYS_CONTROL,
102                 .sc_unit = unit + 1,
103         };
104
105         if(connect(device_fd, (struct sockaddr *)&sc, sizeof(sc)) == -1) {
106                 logger(DEBUG_ALWAYS, LOG_ERR, "Could not connect utun socket: %s\n", strerror(errno));
107                 return false;
108         }
109
110         char name[64] = "";
111         socklen_t len = sizeof(name);
112
113         if(getsockopt(device_fd, SYSPROTO_CONTROL, UTUN_OPT_IFNAME, name, &len)) {
114                 iface = xstrdup(device);
115         } else {
116                 iface = xstrdup(name);
117         }
118
119         device_info = "OS X utun device";
120
121         logger(DEBUG_ALWAYS, LOG_INFO, "%s is a %s", device, device_info);
122
123         return true;
124 }
125 #endif
126
127 static bool setup_device(void) {
128         get_config_string(lookup_config(config_tree, "Device"), &device);
129
130         // Find out if it's supposed to be a tun or a tap device
131
132         char *type;
133
134         if(get_config_string(lookup_config(config_tree, "DeviceType"), &type)) {
135                 if(!strcasecmp(type, "tun"))
136                         /* use default */;
137
138 #ifdef ENABLE_TUNEMU
139                 else if(!strcasecmp(type, "tunemu")) {
140                         device_type = DEVICE_TYPE_TUNEMU;
141                 }
142
143 #endif
144 #ifdef HAVE_NET_IF_UTUN_H
145                 else if(!strcasecmp(type, "utun")) {
146                         device_type = DEVICE_TYPE_UTUN;
147                 }
148
149 #endif
150                 else if(!strcasecmp(type, "tunnohead")) {
151                         device_type = DEVICE_TYPE_TUN;
152                 } else if(!strcasecmp(type, "tunifhead")) {
153                         device_type = DEVICE_TYPE_TUNIFHEAD;
154                 } else if(!strcasecmp(type, "tap")) {
155                         device_type = DEVICE_TYPE_TAP;
156                 } else {
157                         logger(DEBUG_ALWAYS, LOG_ERR, "Unknown device type %s!", type);
158                         return false;
159                 }
160         } else {
161 #ifdef HAVE_NET_IF_UTUN_H
162
163                 if(device && (strncmp(device, "utun", 4) == 0 || strncmp(device, "/dev/utun", 9) == 0)) {
164                         device_type = DEVICE_TYPE_UTUN;
165                 } else
166 #endif
167                         if((device && strstr(device, "tap")) || routing_mode != RMODE_ROUTER) {
168                                 device_type = DEVICE_TYPE_TAP;
169                         }
170         }
171
172         if(routing_mode == RMODE_SWITCH && device_type != DEVICE_TYPE_TAP) {
173                 logger(DEBUG_ALWAYS, LOG_ERR, "Only tap devices support switch mode!");
174                 return false;
175         }
176
177         // Find out which device file to open
178
179         if(!device) {
180                 if(device_type == DEVICE_TYPE_TAP) {
181                         device = xstrdup(DEFAULT_TAP_DEVICE);
182                 } else {
183                         device = xstrdup(DEFAULT_TUN_DEVICE);
184                 }
185         }
186
187         // Open the device
188
189         switch(device_type) {
190 #ifdef ENABLE_TUNEMU
191
192         case DEVICE_TYPE_TUNEMU: {
193                 char dynamic_name[256] = "";
194                 device_fd = tunemu_open(dynamic_name);
195         }
196         break;
197 #endif
198 #ifdef HAVE_NET_IF_UTUN_H
199
200         case DEVICE_TYPE_UTUN:
201                 return setup_utun();
202 #endif
203
204         default:
205                 device_fd = open(device, O_RDWR | O_NONBLOCK);
206         }
207
208         if(device_fd < 0) {
209                 logger(DEBUG_ALWAYS, LOG_ERR, "Could not open %s: %s", device, strerror(errno));
210                 return false;
211         }
212
213 #ifdef FD_CLOEXEC
214         fcntl(device_fd, F_SETFD, FD_CLOEXEC);
215 #endif
216
217         // Guess what the corresponding interface is called
218
219         char *realname = NULL;
220
221 #if defined(HAVE_FDEVNAME)
222         realname = fdevname(device_fd);
223 #elif defined(HAVE_DEVNAME)
224         struct stat buf;
225
226         if(!fstat(device_fd, &buf)) {
227                 realname = devname(buf.st_rdev, S_IFCHR);
228         }
229
230 #endif
231
232         if(!realname) {
233                 realname = device;
234         }
235
236         if(!get_config_string(lookup_config(config_tree, "Interface"), &iface)) {
237                 iface = xstrdup(strrchr(realname, '/') ? strrchr(realname, '/') + 1 : realname);
238         } else if(strcmp(iface, strrchr(realname, '/') ? strrchr(realname, '/') + 1 : realname)) {
239                 logger(DEBUG_ALWAYS, LOG_WARNING, "Warning: Interface does not match Device. $INTERFACE might be set incorrectly.");
240         }
241
242         // Configure the device as best as we can
243
244         switch(device_type) {
245         default:
246                 device_type = DEVICE_TYPE_TUN;
247
248         case DEVICE_TYPE_TUN:
249 #ifdef TUNSIFHEAD
250                 {
251                         const int zero = 0;
252
253                         if(ioctl(device_fd, TUNSIFHEAD, &zero, sizeof(zero)) == -1) {
254                                 logger(DEBUG_ALWAYS, LOG_ERR, "System call `%s' failed: %s", "ioctl", strerror(errno));
255                                 return false;
256                         }
257                 }
258
259 #endif
260 #if defined(TUNSIFMODE) && defined(IFF_BROADCAST) && defined(IFF_MULTICAST)
261                 {
262                         const int mode = IFF_BROADCAST | IFF_MULTICAST;
263                         ioctl(device_fd, TUNSIFMODE, &mode, sizeof(mode));
264                 }
265 #endif
266
267                 device_info = "Generic BSD tun device";
268                 break;
269
270         case DEVICE_TYPE_TUNIFHEAD:
271 #ifdef TUNSIFHEAD
272                 {
273                         const int one = 1;
274
275                         if(ioctl(device_fd, TUNSIFHEAD, &one, sizeof(one)) == -1) {
276                                 logger(DEBUG_ALWAYS, LOG_ERR, "System call `%s' failed: %s", "ioctl", strerror(errno));
277                                 return false;
278                         }
279                 }
280
281 #endif
282 #if defined(TUNSIFMODE) && defined(IFF_BROADCAST) && defined(IFF_MULTICAST)
283                 {
284                         const int mode = IFF_BROADCAST | IFF_MULTICAST;
285                         ioctl(device_fd, TUNSIFMODE, &mode, sizeof(mode));
286                 }
287 #endif
288
289                 device_info = "Generic BSD tun device";
290                 break;
291
292         case DEVICE_TYPE_TAP:
293                 if(routing_mode == RMODE_ROUTER) {
294                         overwrite_mac = true;
295                 }
296
297                 device_info = "Generic BSD tap device";
298 #ifdef TAPGIFNAME
299                 {
300                         struct ifreq ifr;
301
302                         if(ioctl(device_fd, TAPGIFNAME, (void *)&ifr) == 0) {
303                                 if(iface) {
304                                         free(iface);
305                                 }
306
307                                 iface = xstrdup(ifr.ifr_name);
308                         }
309                 }
310
311 #endif
312                 break;
313 #ifdef ENABLE_TUNEMU
314
315         case DEVICE_TYPE_TUNEMU:
316                 device_info = "BSD tunemu device";
317                 break;
318 #endif
319         }
320
321 #ifdef SIOCGIFADDR
322
323         if(overwrite_mac) {
324                 ioctl(device_fd, SIOCGIFADDR, mymac.x);
325         }
326
327 #endif
328
329         logger(DEBUG_ALWAYS, LOG_INFO, "%s is a %s", device, device_info);
330
331         return true;
332 }
333
334 static void close_device(void) {
335         switch(device_type) {
336 #ifdef ENABLE_TUNEMU
337
338         case DEVICE_TYPE_TUNEMU:
339                 tunemu_close(device_fd);
340                 break;
341 #endif
342
343         default:
344                 close(device_fd);
345         }
346
347         device_fd = -1;
348
349         free(device);
350         device = NULL;
351         free(iface);
352         iface = NULL;
353         device_info = NULL;
354 }
355
356 static bool read_packet(vpn_packet_t *packet) {
357         int inlen;
358
359         switch(device_type) {
360         case DEVICE_TYPE_TUN:
361 #ifdef ENABLE_TUNEMU
362         case DEVICE_TYPE_TUNEMU:
363                 if(device_type == DEVICE_TYPE_TUNEMU) {
364                         inlen = tunemu_read(device_fd, DATA(packet) + 14, MTU - 14);
365                 } else
366 #endif
367                         inlen = read(device_fd, DATA(packet) + 14, MTU - 14);
368
369                 if(inlen <= 0) {
370                         logger(DEBUG_ALWAYS, LOG_ERR, "Error while reading from %s %s: %s", device_info,
371                                device, strerror(errno));
372                         return false;
373                 }
374
375                 switch(DATA(packet)[14] >> 4) {
376                 case 4:
377                         DATA(packet)[12] = 0x08;
378                         DATA(packet)[13] = 0x00;
379                         break;
380
381                 case 6:
382                         DATA(packet)[12] = 0x86;
383                         DATA(packet)[13] = 0xDD;
384                         break;
385
386                 default:
387                         logger(DEBUG_TRAFFIC, LOG_ERR,
388                                "Unknown IP version %d while reading packet from %s %s",
389                                DATA(packet)[14] >> 4, device_info, device);
390                         return false;
391                 }
392
393                 memset(DATA(packet), 0, 12);
394                 packet->len = inlen + 14;
395                 break;
396
397         case DEVICE_TYPE_UTUN:
398         case DEVICE_TYPE_TUNIFHEAD: {
399                 if((inlen = read(device_fd, DATA(packet) + 10, MTU - 10)) <= 0) {
400                         logger(DEBUG_ALWAYS, LOG_ERR, "Error while reading from %s %s: %s", device_info,
401                                device, strerror(errno));
402                         return false;
403                 }
404
405                 switch(DATA(packet)[14] >> 4) {
406                 case 4:
407                         DATA(packet)[12] = 0x08;
408                         DATA(packet)[13] = 0x00;
409                         break;
410
411                 case 6:
412                         DATA(packet)[12] = 0x86;
413                         DATA(packet)[13] = 0xDD;
414                         break;
415
416                 default:
417                         logger(DEBUG_TRAFFIC, LOG_ERR,
418                                "Unknown IP version %d while reading packet from %s %s",
419                                DATA(packet)[14] >> 4, device_info, device);
420                         return false;
421                 }
422
423                 memset(DATA(packet), 0, 12);
424                 packet->len = inlen + 10;
425                 break;
426         }
427
428         case DEVICE_TYPE_TAP:
429                 if((inlen = read(device_fd, DATA(packet), MTU)) <= 0) {
430                         logger(DEBUG_ALWAYS, LOG_ERR, "Error while reading from %s %s: %s", device_info,
431                                device, strerror(errno));
432                         return false;
433                 }
434
435                 packet->len = inlen;
436                 break;
437
438         default:
439                 return false;
440         }
441
442         logger(DEBUG_TRAFFIC, LOG_DEBUG, "Read packet of %d bytes from %s",
443                packet->len, device_info);
444
445         return true;
446 }
447
448 static bool write_packet(vpn_packet_t *packet) {
449         logger(DEBUG_TRAFFIC, LOG_DEBUG, "Writing packet of %d bytes to %s",
450                packet->len, device_info);
451
452         switch(device_type) {
453         case DEVICE_TYPE_TUN:
454                 if(write(device_fd, DATA(packet) + 14, packet->len - 14) < 0) {
455                         logger(DEBUG_ALWAYS, LOG_ERR, "Error while writing to %s %s: %s", device_info,
456                                device, strerror(errno));
457                         return false;
458                 }
459
460                 break;
461
462         case DEVICE_TYPE_UTUN:
463         case DEVICE_TYPE_TUNIFHEAD: {
464                 int af = (DATA(packet)[12] << 8) + DATA(packet)[13];
465                 uint32_t type;
466
467                 switch(af) {
468                 case 0x0800:
469                         type = htonl(AF_INET);
470                         break;
471
472                 case 0x86DD:
473                         type = htonl(AF_INET6);
474                         break;
475
476                 default:
477                         logger(DEBUG_TRAFFIC, LOG_ERR,
478                                "Unknown address family %x while writing packet to %s %s",
479                                af, device_info, device);
480                         return false;
481                 }
482
483                 memcpy(DATA(packet) + 10, &type, sizeof(type));
484
485                 if(write(device_fd, DATA(packet) + 10, packet->len - 10) < 0) {
486                         logger(DEBUG_ALWAYS, LOG_ERR, "Can't write to %s %s: %s", device_info, device,
487                                strerror(errno));
488                         return false;
489                 }
490
491                 break;
492         }
493
494         case DEVICE_TYPE_TAP:
495                 if(write(device_fd, DATA(packet), packet->len) < 0) {
496                         logger(DEBUG_ALWAYS, LOG_ERR, "Error while writing to %s %s: %s", device_info,
497                                device, strerror(errno));
498                         return false;
499                 }
500
501                 break;
502
503 #ifdef ENABLE_TUNEMU
504
505         case DEVICE_TYPE_TUNEMU:
506                 if(tunemu_write(device_fd, DATA(packet) + 14, packet->len - 14) < 0) {
507                         logger(DEBUG_ALWAYS, LOG_ERR, "Error while writing to %s %s: %s", device_info,
508                                device, strerror(errno));
509                         return false;
510                 }
511
512                 break;
513 #endif
514
515         default:
516                 return false;
517         }
518
519         return true;
520 }
521
522 const devops_t os_devops = {
523         .setup = setup_device,
524         .close = close_device,
525         .read = read_packet,
526         .write = write_packet,
527 };