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