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