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