Releasing 1.0.27.
[oweals/tinc.git] / 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 #ifdef HAVE_NET_IF_UTUN_H
53         DEVICE_TYPE_UTUN,
54 #endif
55 } device_type_t;
56
57 int device_fd = -1;
58 char *device = NULL;
59 char *iface = NULL;
60 static char *device_info = NULL;
61 static uint64_t device_total_in = 0;
62 static uint64_t device_total_out = 0;
63 #if defined(ENABLE_TUNEMU)
64 static device_type_t device_type = DEVICE_TYPE_TUNEMU;
65 #elif defined(HAVE_OPENBSD) || defined(HAVE_FREEBSD) || defined(HAVE_DRAGONFLY)
66 static device_type_t device_type = DEVICE_TYPE_TUNIFHEAD;
67 #else
68 static device_type_t device_type = DEVICE_TYPE_TUN;
69 #endif
70
71 #ifdef HAVE_NET_IF_UTUN_H
72 static bool setup_utun(void) {
73         device_fd = socket(PF_SYSTEM, SOCK_DGRAM, SYSPROTO_CONTROL);
74         if(device_fd == -1) {
75                 logger(LOG_ERR, "Could not open PF_SYSTEM socket: %s\n", strerror(errno));
76                 return false;
77         }
78
79         struct ctl_info info = {};
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         if(p) {
90                 unit = strtol(p + 4, &e, 10);
91                 if(!e)
92                         unit = -1;
93         }
94
95         struct sockaddr_ctl sc = {
96                 .sc_id = info.ctl_id,
97                 .sc_len = sizeof sc,
98                 .sc_family = AF_SYSTEM,
99                 .ss_sysaddr = AF_SYS_CONTROL,
100                 .sc_unit = unit + 1,
101         };
102
103         if(connect(device_fd, (struct sockaddr *)&sc, sizeof(sc)) == -1) {
104                 logger(LOG_ERR, "Could not connect utun socket: %s\n", strerror(errno));
105                 return false;
106         }
107
108         char name[64] = "";
109         socklen_t len = sizeof name;
110         if(getsockopt(device_fd, SYSPROTO_CONTROL, UTUN_OPT_IFNAME, name, &len)) {
111                 iface = xstrdup(device);
112         } else {
113                 iface = xstrdup(name);
114         }
115
116         device_info = "OS X utun device";
117
118         logger(LOG_INFO, "%s is a %s", device, device_info);
119
120         return true;
121 }
122 #endif
123
124 static bool setup_device(void) {
125         // Find out which device file to open
126
127         if(!get_config_string(lookup_config(config_tree, "Device"), &device)) {
128                 if(routing_mode == RMODE_ROUTER)
129                         device = xstrdup(DEFAULT_TUN_DEVICE);
130                 else
131                         device = xstrdup(DEFAULT_TAP_DEVICE);
132         }
133
134         // Find out if it's supposed to be a tun or a tap device
135
136         char *type;
137
138         if(get_config_string(lookup_config(config_tree, "DeviceType"), &type)) {
139                 if(!strcasecmp(type, "tun"))
140                         /* use default */;      
141 #ifdef ENABLE_TUNEMU
142                 else if(!strcasecmp(type, "tunemu"))
143                         device_type = DEVICE_TYPE_TUNEMU;
144 #endif
145 #ifdef HAVE_NET_IF_UTUN_H
146                 else if(!strcasecmp(type, "utun"))
147                         device_type = DEVICE_TYPE_UTUN;
148 #endif
149                 else if(!strcasecmp(type, "tunnohead"))
150                         device_type = DEVICE_TYPE_TUN;
151                 else if(!strcasecmp(type, "tunifhead"))
152                         device_type = DEVICE_TYPE_TUNIFHEAD;
153                 else if(!strcasecmp(type, "tap"))
154                         device_type = DEVICE_TYPE_TAP;
155                 else {
156                         logger(LOG_ERR, "Unknown device type %s!", type);
157                         return false;
158                 }
159         } else {
160 #ifdef HAVE_NET_IF_UTUN_H
161                 if(strncmp(device, "utun", 4) == 0 || strncmp(device, "/dev/utun", 9) == 0)
162                         device_type = DEVICE_TYPE_UTUN;
163                 else
164 #endif
165                 if(strstr(device, "tap") || routing_mode != RMODE_ROUTER)
166                         device_type = DEVICE_TYPE_TAP;
167         }
168
169         if(routing_mode == RMODE_SWITCH && device_type != DEVICE_TYPE_TAP) {
170                 logger(LOG_ERR, "Only tap devices support switch mode!");
171                 return false;
172         }
173
174         // Open the device
175
176         switch(device_type) {
177 #ifdef ENABLE_TUNEMU
178                 case DEVICE_TYPE_TUNEMU: {
179                         char dynamic_name[256] = "";
180                         device_fd = tunemu_open(dynamic_name);
181                 }
182                         break;
183 #endif
184 #ifdef HAVE_NET_IF_UTUN_H
185                 case DEVICE_TYPE_UTUN:
186                         return setup_utun();
187 #endif
188                 default:
189                         device_fd = open(device, O_RDWR | O_NONBLOCK);
190         }
191
192         if(device_fd < 0) {
193                 logger(LOG_ERR, "Could not open %s: %s", device, strerror(errno));
194                 return false;
195         }
196
197 #ifdef FD_CLOEXEC
198         fcntl(device_fd, F_SETFD, FD_CLOEXEC);
199 #endif
200
201         // Guess what the corresponding interface is called
202
203         char *realname;
204
205 #if defined(HAVE_FDEVNAME)
206         realname = fdevname(device_fd) ? : device;
207 #elif defined(HAVE_DEVNAME)
208         struct stat buf;
209         if(!fstat(device_fd, &buf))
210                 realname = devname(buf.st_rdev, S_IFCHR) ? : device;
211 #else
212         realname = device;
213 #endif
214
215         if(!get_config_string(lookup_config(config_tree, "Interface"), &iface))
216                 iface = xstrdup(strrchr(realname, '/') ? strrchr(realname, '/') + 1 : realname);
217         else if(strcmp(iface, strrchr(realname, '/') ? strrchr(realname, '/') + 1 : realname))
218                 logger(LOG_WARNING, "Warning: Interface does not match Device. $INTERFACE might be set incorrectly.");
219
220         // Configure the device as best as we can
221
222         switch(device_type) {
223                 default:
224                         device_type = DEVICE_TYPE_TUN;
225                 case DEVICE_TYPE_TUN:
226 #ifdef TUNSIFHEAD
227                 {       
228                         const int zero = 0;
229                         if(ioctl(device_fd, TUNSIFHEAD, &zero, sizeof zero) == -1) {
230                                 logger(LOG_ERR, "System call `%s' failed: %s", "ioctl", strerror(errno));
231                                 return false;
232                         }
233                 }
234 #endif
235 #if defined(TUNSIFMODE) && defined(IFF_BROADCAST) && defined(IFF_MULTICAST)
236                 {
237                         const int mode = IFF_BROADCAST | IFF_MULTICAST;
238                         ioctl(device_fd, TUNSIFMODE, &mode, sizeof mode);
239                 }
240 #endif
241
242                         device_info = "Generic BSD tun device";
243                         break;
244                 case DEVICE_TYPE_TUNIFHEAD:
245 #ifdef TUNSIFHEAD
246                 {
247                         const int one = 1;
248                         if(ioctl(device_fd, TUNSIFHEAD, &one, sizeof one) == -1) {
249                                 logger(LOG_ERR, "System call `%s' failed: %s", "ioctl", strerror(errno));
250                                 return false;
251                         }
252                 }
253 #endif
254 #if defined(TUNSIFMODE) && defined(IFF_BROADCAST) && defined(IFF_MULTICAST)
255                 {
256                                 const int mode = IFF_BROADCAST | IFF_MULTICAST;
257                                 ioctl(device_fd, TUNSIFMODE, &mode, sizeof mode);
258                 }
259 #endif
260
261                         device_info = "Generic BSD tun device";
262                         break;
263                 case DEVICE_TYPE_TAP:
264                         if(routing_mode == RMODE_ROUTER)
265                                 overwrite_mac = true;
266                         device_info = "Generic BSD tap device";
267 #ifdef TAPGIFNAME
268                         {
269                                 struct ifreq ifr;
270                                 if(ioctl(device_fd, TAPGIFNAME, (void*)&ifr) == 0) {
271                                         if(iface)
272                                                 free(iface);
273                                         iface = xstrdup(ifr.ifr_name);
274                                 }
275                         }
276                         
277 #endif
278                         break;
279 #ifdef ENABLE_TUNEMU
280                 case DEVICE_TYPE_TUNEMU:
281                         device_info = "BSD tunemu device";
282                         break;
283 #endif
284         }
285
286 #ifdef SIOCGIFADDR
287         if(overwrite_mac)
288                 ioctl(device_fd, SIOCGIFADDR, mymac.x);
289 #endif
290
291         logger(LOG_INFO, "%s is a %s", device, device_info);
292
293         return true;
294 }
295
296 static void close_device(void) {
297         switch(device_type) {
298 #ifdef ENABLE_TUNEMU
299                 case DEVICE_TYPE_TUNEMU:
300                         tunemu_close(device_fd);
301                         break;
302 #endif
303                 default:
304                         close(device_fd);
305         }
306
307         free(device);
308         free(iface);
309 }
310
311 static bool read_packet(vpn_packet_t *packet) {
312         int lenin;
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                                 lenin = tunemu_read(device_fd, packet->data + 14, MTU - 14);
320                         else
321 #endif
322                                 lenin = read(device_fd, packet->data + 14, MTU - 14);
323
324                         if(lenin <= 0) {
325                                 logger(LOG_ERR, "Error while reading from %s %s: %s", device_info,
326                                            device, strerror(errno));
327                                 return false;
328                         }
329
330                         switch(packet->data[14] >> 4) {
331                                 case 4:
332                                         packet->data[12] = 0x08;
333                                         packet->data[13] = 0x00;
334                                         break;
335                                 case 6:
336                                         packet->data[12] = 0x86;
337                                         packet->data[13] = 0xDD;
338                                         break;
339                                 default:
340                                         ifdebug(TRAFFIC) logger(LOG_ERR,
341                                                            "Unknown IP version %d while reading packet from %s %s",
342                                                            packet->data[14] >> 4, device_info, device);
343                                         return false;
344                         }
345
346                         memset(packet->data, 0, 12);
347                         packet->len = lenin + 14;
348                         break;
349
350                 case DEVICE_TYPE_UTUN:
351                 case DEVICE_TYPE_TUNIFHEAD: {
352                         if((lenin = read(device_fd, packet->data + 10, MTU - 10)) <= 0) {
353                                 logger(LOG_ERR, "Error while reading from %s %s: %s", device_info,
354                                            device, strerror(errno));
355                                 return false;
356                         }
357
358                         switch(packet->data[14] >> 4) {
359                                 case 4:
360                                         packet->data[12] = 0x08;
361                                         packet->data[13] = 0x00;
362                                         break;
363                                 case 6:
364                                         packet->data[12] = 0x86;
365                                         packet->data[13] = 0xDD;
366                                         break;
367                                 default:
368                                         ifdebug(TRAFFIC) logger(LOG_ERR,
369                                                            "Unknown IP version %d while reading packet from %s %s",
370                                                            packet->data[14] >> 4, device_info, device);
371                                         return false;
372                         }
373
374                         memset(packet->data, 0, 12);
375                         packet->len = lenin + 10;
376                         break;
377                 }
378
379                 case DEVICE_TYPE_TAP:
380                         if((lenin = read(device_fd, packet->data, MTU)) <= 0) {
381                                 logger(LOG_ERR, "Error while reading from %s %s: %s", device_info,
382                                            device, strerror(errno));
383                                 return false;
384                         }
385
386                         packet->len = lenin;
387                         break;
388
389                 default:
390                         return false;
391         }
392                 
393         device_total_in += packet->len;
394
395         ifdebug(TRAFFIC) logger(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         ifdebug(TRAFFIC) logger(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, packet->data + 14, packet->len - 14) < 0) {
408                                 logger(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 = (packet->data[12] << 8) + packet->data[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                                         ifdebug(TRAFFIC) logger(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(packet->data + 10, &type, sizeof type);
434
435                         if(write(device_fd, packet->data + 10, packet->len - 10) < 0) {
436                                 logger(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, packet->data, packet->len) < 0) {
445                                 logger(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, packet->data + 14, packet->len - 14) < 0) {
454                                 logger(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         device_total_out += packet->len;
466
467         return true;
468 }
469
470 static void dump_device_stats(void) {
471         logger(LOG_DEBUG, "Statistics for %s %s:", device_info, device);
472         logger(LOG_DEBUG, " total bytes in:  %10"PRIu64, device_total_in);
473         logger(LOG_DEBUG, " total bytes out: %10"PRIu64, device_total_out);
474 }
475
476 const devops_t os_devops = {
477         .setup = setup_device,
478         .close = close_device,
479         .read = read_packet,
480         .write = write_packet,
481         .dump_stats = dump_device_stats,
482 };