Remove unused argument from tunemu_write.
[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 "../route.h"
28 #include "../xalloc.h"
29
30 #ifdef ENABLE_TUNEMU
31 #include "bsd/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         strlcpy(info.ctl_name, UTUN_CONTROL_NAME, sizeof(info.ctl_name));
82
83         if(ioctl(device_fd, CTLIOCGINFO, &info) == -1) {
84                 logger(DEBUG_ALWAYS, LOG_ERR, "ioctl(CTLIOCGINFO) failed: %s", strerror(errno));
85                 return false;
86         }
87
88         int unit = -1;
89         char *p = strstr(device, "utun"), *e = NULL;
90
91         if(p) {
92                 unit = strtol(p + 4, &e, 10);
93
94                 if(!e) {
95                         unit = -1;
96                 }
97         }
98
99         struct sockaddr_ctl sc = {
100                 .sc_id = info.ctl_id,
101                 .sc_len = sizeof(sc),
102                 .sc_family = AF_SYSTEM,
103                 .ss_sysaddr = AF_SYS_CONTROL,
104                 .sc_unit = unit + 1,
105         };
106
107         if(connect(device_fd, (struct sockaddr *)&sc, sizeof(sc)) == -1) {
108                 logger(DEBUG_ALWAYS, LOG_ERR, "Could not connect utun socket: %s\n", strerror(errno));
109                 return false;
110         }
111
112         char name[64] = "";
113         socklen_t len = sizeof(name);
114
115         if(getsockopt(device_fd, SYSPROTO_CONTROL, UTUN_OPT_IFNAME, name, &len)) {
116                 iface = xstrdup(device);
117         } else {
118                 iface = xstrdup(name);
119         }
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                                 free(iface);
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(DEBUG_ALWAYS, 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         device_fd = -1;
345
346         free(device);
347         device = NULL;
348         free(iface);
349         iface = NULL;
350         device_info = NULL;
351 }
352
353 static bool read_packet(vpn_packet_t *packet) {
354         int inlen;
355
356         switch(device_type) {
357         case DEVICE_TYPE_TUN:
358 #ifdef ENABLE_TUNEMU
359         case DEVICE_TYPE_TUNEMU:
360                 if(device_type == DEVICE_TYPE_TUNEMU) {
361                         inlen = tunemu_read(device_fd, DATA(packet) + 14, MTU - 14);
362                 } else
363 #endif
364                         inlen = read(device_fd, DATA(packet) + 14, MTU - 14);
365
366                 if(inlen <= 0) {
367                         logger(DEBUG_ALWAYS, LOG_ERR, "Error while reading from %s %s: %s", device_info,
368                                device, strerror(errno));
369                         return false;
370                 }
371
372                 switch(DATA(packet)[14] >> 4) {
373                 case 4:
374                         DATA(packet)[12] = 0x08;
375                         DATA(packet)[13] = 0x00;
376                         break;
377
378                 case 6:
379                         DATA(packet)[12] = 0x86;
380                         DATA(packet)[13] = 0xDD;
381                         break;
382
383                 default:
384                         logger(DEBUG_TRAFFIC, LOG_ERR,
385                                "Unknown IP version %d while reading packet from %s %s",
386                                DATA(packet)[14] >> 4, device_info, device);
387                         return false;
388                 }
389
390                 memset(DATA(packet), 0, 12);
391                 packet->len = inlen + 14;
392                 break;
393
394         case DEVICE_TYPE_UTUN:
395         case DEVICE_TYPE_TUNIFHEAD: {
396                 if((inlen = read(device_fd, DATA(packet) + 10, MTU - 10)) <= 0) {
397                         logger(DEBUG_ALWAYS, LOG_ERR, "Error while reading from %s %s: %s", device_info,
398                                device, strerror(errno));
399                         return false;
400                 }
401
402                 switch(DATA(packet)[14] >> 4) {
403                 case 4:
404                         DATA(packet)[12] = 0x08;
405                         DATA(packet)[13] = 0x00;
406                         break;
407
408                 case 6:
409                         DATA(packet)[12] = 0x86;
410                         DATA(packet)[13] = 0xDD;
411                         break;
412
413                 default:
414                         logger(DEBUG_TRAFFIC, LOG_ERR,
415                                "Unknown IP version %d while reading packet from %s %s",
416                                DATA(packet)[14] >> 4, device_info, device);
417                         return false;
418                 }
419
420                 memset(DATA(packet), 0, 12);
421                 packet->len = inlen + 10;
422                 break;
423         }
424
425         case DEVICE_TYPE_TAP:
426                 if((inlen = read(device_fd, DATA(packet), MTU)) <= 0) {
427                         logger(DEBUG_ALWAYS, LOG_ERR, "Error while reading from %s %s: %s", device_info,
428                                device, strerror(errno));
429                         return false;
430                 }
431
432                 packet->len = inlen;
433                 break;
434
435         default:
436                 return false;
437         }
438
439         logger(DEBUG_TRAFFIC, LOG_DEBUG, "Read packet of %d bytes from %s",
440                packet->len, device_info);
441
442         return true;
443 }
444
445 static bool write_packet(vpn_packet_t *packet) {
446         logger(DEBUG_TRAFFIC, LOG_DEBUG, "Writing packet of %d bytes to %s",
447                packet->len, device_info);
448
449         switch(device_type) {
450         case DEVICE_TYPE_TUN:
451                 if(write(device_fd, DATA(packet) + 14, packet->len - 14) < 0) {
452                         logger(DEBUG_ALWAYS, LOG_ERR, "Error while writing to %s %s: %s", device_info,
453                                device, strerror(errno));
454                         return false;
455                 }
456
457                 break;
458
459         case DEVICE_TYPE_UTUN:
460         case DEVICE_TYPE_TUNIFHEAD: {
461                 int af = (DATA(packet)[12] << 8) + DATA(packet)[13];
462                 uint32_t type;
463
464                 switch(af) {
465                 case 0x0800:
466                         type = htonl(AF_INET);
467                         break;
468
469                 case 0x86DD:
470                         type = htonl(AF_INET6);
471                         break;
472
473                 default:
474                         logger(DEBUG_TRAFFIC, LOG_ERR,
475                                "Unknown address family %x while writing packet to %s %s",
476                                af, device_info, device);
477                         return false;
478                 }
479
480                 memcpy(DATA(packet) + 10, &type, sizeof(type));
481
482                 if(write(device_fd, DATA(packet) + 10, packet->len - 10) < 0) {
483                         logger(DEBUG_ALWAYS, LOG_ERR, "Can't write to %s %s: %s", device_info, device,
484                                strerror(errno));
485                         return false;
486                 }
487
488                 break;
489         }
490
491         case DEVICE_TYPE_TAP:
492                 if(write(device_fd, DATA(packet), packet->len) < 0) {
493                         logger(DEBUG_ALWAYS, LOG_ERR, "Error while writing to %s %s: %s", device_info,
494                                device, strerror(errno));
495                         return false;
496                 }
497
498                 break;
499
500 #ifdef ENABLE_TUNEMU
501
502         case DEVICE_TYPE_TUNEMU:
503                 if(tunemu_write(DATA(packet) + 14, packet->len - 14) < 0) {
504                         logger(DEBUG_ALWAYS, LOG_ERR, "Error while writing to %s %s: %s", device_info,
505                                device, strerror(errno));
506                         return false;
507                 }
508
509                 break;
510 #endif
511
512         default:
513                 return false;
514         }
515
516         return true;
517 }
518
519 const devops_t os_devops = {
520         .setup = setup_device,
521         .close = close_device,
522         .read = read_packet,
523         .write = write_packet,
524 };