Update copyright notices.
[tinc] / src / bsd / device.c
1 /*
2     device.c -- Interaction BSD tun/tap device
3     Copyright (C) 2001-2005 Ivo Timmermans,
4                   2001-2021 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 #if defined(HAVE_FREEBSD) || defined(HAVE_DRAGONFLY)
44 #define DEFAULT_TUN_DEVICE "/dev/tun"  // Use the autoclone device
45 #define DEFAULT_TAP_DEVICE "/dev/tap"
46 #else
47 #define DEFAULT_TUN_DEVICE "/dev/tun0"
48 #define DEFAULT_TAP_DEVICE "/dev/tap0"
49 #endif
50
51 typedef enum device_type {
52         DEVICE_TYPE_TUN,
53         DEVICE_TYPE_TUNIFHEAD,
54         DEVICE_TYPE_TAP,
55 #ifdef ENABLE_TUNEMU
56         DEVICE_TYPE_TUNEMU,
57 #endif
58         DEVICE_TYPE_UTUN,
59 } device_type_t;
60
61 int device_fd = -1;
62 char *device = NULL;
63 char *iface = NULL;
64 static const char *device_info = "OS X utun device";
65 #if defined(ENABLE_TUNEMU)
66 static device_type_t device_type = DEVICE_TYPE_TUNEMU;
67 #elif defined(HAVE_OPENBSD) || defined(HAVE_FREEBSD) || defined(HAVE_DRAGONFLY)
68 static device_type_t device_type = DEVICE_TYPE_TUNIFHEAD;
69 #else
70 static device_type_t device_type = DEVICE_TYPE_TUN;
71 #endif
72
73 #ifdef HAVE_NET_IF_UTUN_H
74 static bool setup_utun(void) {
75         device_fd = socket(PF_SYSTEM, SOCK_DGRAM, SYSPROTO_CONTROL);
76
77         if(device_fd == -1) {
78                 logger(DEBUG_ALWAYS, LOG_ERR, "Could not open PF_SYSTEM socket: %s\n", strerror(errno));
79                 return false;
80         }
81
82         struct ctl_info info = {};
83
84         strlcpy(info.ctl_name, UTUN_CONTROL_NAME, sizeof(info.ctl_name));
85
86         if(ioctl(device_fd, CTLIOCGINFO, &info) == -1) {
87                 logger(DEBUG_ALWAYS, LOG_ERR, "ioctl(CTLIOCGINFO) failed: %s", strerror(errno));
88                 return false;
89         }
90
91         int unit = -1;
92         char *p = strstr(device, "utun"), *e = NULL;
93
94         if(p) {
95                 unit = strtol(p + 4, &e, 10);
96
97                 if(!e) {
98                         unit = -1;
99                 }
100         }
101
102         struct sockaddr_ctl sc = {
103                 .sc_id = info.ctl_id,
104                 .sc_len = sizeof(sc),
105                 .sc_family = AF_SYSTEM,
106                 .ss_sysaddr = AF_SYS_CONTROL,
107                 .sc_unit = unit + 1,
108         };
109
110         if(connect(device_fd, (struct sockaddr *)&sc, sizeof(sc)) == -1) {
111                 logger(DEBUG_ALWAYS, LOG_ERR, "Could not connect utun socket: %s\n", strerror(errno));
112                 return false;
113         }
114
115         char name[64] = "";
116         socklen_t len = sizeof(name);
117
118         if(getsockopt(device_fd, SYSPROTO_CONTROL, UTUN_OPT_IFNAME, name, &len)) {
119                 iface = xstrdup(device);
120         } else {
121                 iface = xstrdup(name);
122         }
123
124         logger(DEBUG_ALWAYS, LOG_INFO, "%s is a %s", device, device_info);
125
126         return true;
127 }
128 #endif
129
130 static bool setup_device(void) {
131         get_config_string(lookup_config(config_tree, "Device"), &device);
132
133         // Find out if it's supposed to be a tun or a tap device
134
135         char *type;
136
137         if(get_config_string(lookup_config(config_tree, "DeviceType"), &type)) {
138                 if(!strcasecmp(type, "tun"))
139                         /* use default */;
140
141 #ifdef ENABLE_TUNEMU
142                 else if(!strcasecmp(type, "tunemu")) {
143                         device_type = DEVICE_TYPE_TUNEMU;
144                 }
145
146 #endif
147 #ifdef HAVE_NET_IF_UTUN_H
148                 else if(!strcasecmp(type, "utun")) {
149                         device_type = DEVICE_TYPE_UTUN;
150                 }
151
152 #endif
153                 else if(!strcasecmp(type, "tunnohead")) {
154                         device_type = DEVICE_TYPE_TUN;
155                 } else if(!strcasecmp(type, "tunifhead")) {
156                         device_type = DEVICE_TYPE_TUNIFHEAD;
157                 } else if(!strcasecmp(type, "tap")) {
158                         device_type = DEVICE_TYPE_TAP;
159                 } else {
160                         logger(DEBUG_ALWAYS, LOG_ERR, "Unknown device type %s!", type);
161                         return false;
162                 }
163         } else {
164 #ifdef HAVE_NET_IF_UTUN_H
165
166                 if(device && (strncmp(device, "utun", 4) == 0 || strncmp(device, "/dev/utun", 9) == 0)) {
167                         device_type = DEVICE_TYPE_UTUN;
168                 } else
169 #endif
170                         if((device && strstr(device, "tap")) || routing_mode != RMODE_ROUTER) {
171                                 device_type = DEVICE_TYPE_TAP;
172                         }
173         }
174
175         if(routing_mode == RMODE_SWITCH && device_type != DEVICE_TYPE_TAP) {
176                 logger(DEBUG_ALWAYS, LOG_ERR, "Only tap devices support switch mode!");
177                 return false;
178         }
179
180         // Find out which device file to open
181
182         if(!device) {
183                 if(device_type == DEVICE_TYPE_TAP) {
184                         device = xstrdup(DEFAULT_TAP_DEVICE);
185                 } else {
186                         device = xstrdup(DEFAULT_TUN_DEVICE);
187                 }
188         }
189
190         // Open the device
191
192         switch(device_type) {
193 #ifdef ENABLE_TUNEMU
194
195         case DEVICE_TYPE_TUNEMU: {
196                 char dynamic_name[256] = "";
197                 device_fd = tunemu_open(dynamic_name);
198         }
199         break;
200 #endif
201 #ifdef HAVE_NET_IF_UTUN_H
202
203         case DEVICE_TYPE_UTUN:
204                 return setup_utun();
205 #endif
206
207         default:
208                 device_fd = open(device, O_RDWR | O_NONBLOCK);
209         }
210
211         if(device_fd < 0) {
212                 logger(DEBUG_ALWAYS, LOG_ERR, "Could not open %s: %s", device, strerror(errno));
213                 return false;
214         }
215
216 #ifdef FD_CLOEXEC
217         fcntl(device_fd, F_SETFD, FD_CLOEXEC);
218 #endif
219
220         // Guess what the corresponding interface is called
221
222         char *realname = NULL;
223
224 #if defined(HAVE_FDEVNAME)
225         realname = fdevname(device_fd);
226 #elif defined(HAVE_DEVNAME)
227         struct stat buf;
228
229         if(!fstat(device_fd, &buf)) {
230                 realname = devname(buf.st_rdev, S_IFCHR);
231         }
232
233 #endif
234
235         if(!realname) {
236                 realname = device;
237         }
238
239         if(!get_config_string(lookup_config(config_tree, "Interface"), &iface)) {
240                 iface = xstrdup(strrchr(realname, '/') ? strrchr(realname, '/') + 1 : realname);
241         } else if(strcmp(iface, strrchr(realname, '/') ? strrchr(realname, '/') + 1 : realname)) {
242                 logger(DEBUG_ALWAYS, LOG_WARNING, "Warning: Interface does not match Device. $INTERFACE might be set incorrectly.");
243         }
244
245         // Configure the device as best as we can
246
247         switch(device_type) {
248         default:
249                 device_type = DEVICE_TYPE_TUN;
250
251         case DEVICE_TYPE_TUN:
252 #ifdef TUNSIFHEAD
253                 {
254                         const int zero = 0;
255
256                         if(ioctl(device_fd, TUNSIFHEAD, &zero, sizeof(zero)) == -1) {
257                                 logger(DEBUG_ALWAYS, LOG_ERR, "System call `%s' failed: %s", "ioctl", strerror(errno));
258                                 return false;
259                         }
260                 }
261
262 #endif
263 #if defined(TUNSIFMODE) && defined(IFF_BROADCAST) && defined(IFF_MULTICAST)
264                 {
265                         const int mode = IFF_BROADCAST | IFF_MULTICAST;
266                         ioctl(device_fd, TUNSIFMODE, &mode, sizeof(mode));
267                 }
268 #endif
269
270                 device_info = "Generic BSD tun device";
271                 break;
272
273         case DEVICE_TYPE_TUNIFHEAD:
274 #ifdef TUNSIFHEAD
275                 {
276                         const int one = 1;
277
278                         if(ioctl(device_fd, TUNSIFHEAD, &one, sizeof(one)) == -1) {
279                                 logger(DEBUG_ALWAYS, LOG_ERR, "System call `%s' failed: %s", "ioctl", strerror(errno));
280                                 return false;
281                         }
282                 }
283
284 #endif
285 #if defined(TUNSIFMODE) && defined(IFF_BROADCAST) && defined(IFF_MULTICAST)
286                 {
287                         const int mode = IFF_BROADCAST | IFF_MULTICAST;
288                         ioctl(device_fd, TUNSIFMODE, &mode, sizeof(mode));
289                 }
290 #endif
291
292                 device_info = "Generic BSD tun device";
293                 break;
294
295         case DEVICE_TYPE_TAP:
296                 if(routing_mode == RMODE_ROUTER) {
297                         overwrite_mac = true;
298                 }
299
300                 device_info = "Generic BSD tap device";
301 #ifdef TAPGIFNAME
302                 {
303                         struct ifreq ifr;
304
305                         if(ioctl(device_fd, TAPGIFNAME, (void *)&ifr) == 0) {
306                                 free(iface);
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 };