Enable silent builds by default.
[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 "../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 #define DEFAULT_TUN_DEVICE "/dev/tun0"
37 #define DEFAULT_TAP_DEVICE "/dev/tap0"
38
39 typedef enum device_type {
40         DEVICE_TYPE_TUN,
41         DEVICE_TYPE_TUNIFHEAD,
42         DEVICE_TYPE_TAP,
43 #ifdef ENABLE_TUNEMU
44         DEVICE_TYPE_TUNEMU,
45 #endif
46 } device_type_t;
47
48 int device_fd = -1;
49 char *device = NULL;
50 char *iface = NULL;
51 static char *device_info = NULL;
52 static uint64_t device_total_in = 0;
53 static uint64_t device_total_out = 0;
54 #if defined(ENABLE_TUNEMU)
55 static device_type_t device_type = DEVICE_TYPE_TUNEMU;
56 #elif defined(HAVE_OPENBSD) || defined(HAVE_FREEBSD) || defined(HAVE_DRAGONFLY)
57 static device_type_t device_type = DEVICE_TYPE_TUNIFHEAD;
58 #else
59 static device_type_t device_type = DEVICE_TYPE_TUN;
60 #endif
61
62 static bool setup_device(void) {
63         // Find out which device file to open
64
65         if(!get_config_string(lookup_config(config_tree, "Device"), &device)) {
66                 if(routing_mode == RMODE_ROUTER)
67                         device = xstrdup(DEFAULT_TUN_DEVICE);
68                 else
69                         device = xstrdup(DEFAULT_TAP_DEVICE);
70         }
71
72         // Find out if it's supposed to be a tun or a tap device
73
74         char *type;
75
76         if(get_config_string(lookup_config(config_tree, "DeviceType"), &type)) {
77                 if(!strcasecmp(type, "tun"))
78                         /* use default */;      
79 #ifdef ENABLE_TUNEMU
80                 else if(!strcasecmp(type, "tunemu"))
81                         device_type = DEVICE_TYPE_TUNEMU;
82 #endif
83                 else if(!strcasecmp(type, "tunnohead"))
84                         device_type = DEVICE_TYPE_TUN;
85                 else if(!strcasecmp(type, "tunifhead"))
86                         device_type = DEVICE_TYPE_TUNIFHEAD;
87                 else if(!strcasecmp(type, "tap"))
88                         device_type = DEVICE_TYPE_TAP;
89                 else {
90                         logger(LOG_ERR, "Unknown device type %s!", type);
91                         return false;
92                 }
93         } else {
94                 if(strstr(device, "tap") || routing_mode != RMODE_ROUTER)
95                         device_type = DEVICE_TYPE_TAP;
96         }
97
98         // Open the device
99
100         switch(device_type) {
101 #ifdef ENABLE_TUNEMU
102                 case DEVICE_TYPE_TUNEMU: {
103                         char dynamic_name[256] = "";
104                         device_fd = tunemu_open(dynamic_name);
105                 }
106                         break;
107 #endif
108                 default:
109                         device_fd = open(device, O_RDWR | O_NONBLOCK);
110         }
111
112         if(device_fd < 0) {
113                 logger(LOG_ERR, "Could not open %s: %s", device, strerror(errno));
114                 return false;
115         }
116
117 #ifdef FD_CLOEXEC
118         fcntl(device_fd, F_SETFD, FD_CLOEXEC);
119 #endif
120
121         // Guess what the corresponding interface is called
122
123         char *realname;
124
125 #if defined(HAVE_FDEVNAME)
126         realname = fdevname(device_fd) ? : device;
127 #elif defined(HAVE_DEVNAME)
128         struct stat buf;
129         if(!fstat(device_fd, &buf))
130                 realname = devname(buf.st_rdev, S_IFCHR) ? : device;
131 #else
132         realname = device;
133 #endif
134
135         if(!get_config_string(lookup_config(config_tree, "Interface"), &iface))
136                 iface = xstrdup(strrchr(realname, '/') ? strrchr(realname, '/') + 1 : realname);
137         else if(strcmp(iface, strrchr(realname, '/') ? strrchr(realname, '/') + 1 : realname))
138                 logger(LOG_WARNING, "Warning: Interface does not match Device. $INTERFACE might be set incorrectly.");
139
140         // Configure the device as best as we can
141
142         switch(device_type) {
143                 default:
144                         device_type = DEVICE_TYPE_TUN;
145                 case DEVICE_TYPE_TUN:
146 #ifdef TUNSIFHEAD
147                 {       
148                         const int zero = 0;
149                         if(ioctl(device_fd, TUNSIFHEAD, &zero, sizeof zero) == -1) {
150                                 logger(LOG_ERR, "System call `%s' failed: %s", "ioctl", strerror(errno));
151                                 return false;
152                         }
153                 }
154 #endif
155 #if defined(TUNSIFMODE) && defined(IFF_BROADCAST) && defined(IFF_MULTICAST)
156                 {
157                         const int mode = IFF_BROADCAST | IFF_MULTICAST;
158                         ioctl(device_fd, TUNSIFMODE, &mode, sizeof mode);
159                 }
160 #endif
161
162                         device_info = "Generic BSD tun device";
163                         break;
164                 case DEVICE_TYPE_TUNIFHEAD:
165 #ifdef TUNSIFHEAD
166                 {
167                         const int one = 1;
168                         if(ioctl(device_fd, TUNSIFHEAD, &one, sizeof one) == -1) {
169                                 logger(LOG_ERR, "System call `%s' failed: %s", "ioctl", strerror(errno));
170                                 return false;
171                         }
172                 }
173 #endif
174 #if defined(TUNSIFMODE) && defined(IFF_BROADCAST) && defined(IFF_MULTICAST)
175                 {
176                                 const int mode = IFF_BROADCAST | IFF_MULTICAST;
177                                 ioctl(device_fd, TUNSIFMODE, &mode, sizeof mode);
178                 }
179 #endif
180
181                         device_info = "Generic BSD tun device";
182                         break;
183                 case DEVICE_TYPE_TAP:
184                         if(routing_mode == RMODE_ROUTER)
185                                 overwrite_mac = true;
186                         device_info = "Generic BSD tap device";
187 #ifdef TAPGIFNAME
188                         {
189                                 struct ifreq ifr;
190                                 if(ioctl(device_fd, TAPGIFNAME, (void*)&ifr) == 0) {
191                                         if(iface)
192                                                 free(iface);
193                                         iface = xstrdup(ifr.ifr_name);
194                                 }
195                         }
196                         
197 #endif
198                         break;
199 #ifdef ENABLE_TUNEMU
200                 case DEVICE_TYPE_TUNEMU:
201                         device_info = "BSD tunemu device";
202                         break;
203 #endif
204         }
205
206 #ifdef SIOCGIFADDR
207         if(overwrite_mac)
208                 ioctl(device_fd, SIOCGIFADDR, mymac.x);
209 #endif
210
211         logger(LOG_INFO, "%s is a %s", device, device_info);
212
213         return true;
214 }
215
216 static void close_device(void) {
217         switch(device_type) {
218 #ifdef ENABLE_TUNEMU
219                 case DEVICE_TYPE_TUNEMU:
220                         tunemu_close(device_fd);
221                         break;
222 #endif
223                 default:
224                         close(device_fd);
225         }
226
227         free(device);
228         free(iface);
229 }
230
231 static bool read_packet(vpn_packet_t *packet) {
232         int lenin;
233
234         switch(device_type) {
235                 case DEVICE_TYPE_TUN:
236 #ifdef ENABLE_TUNEMU
237                 case DEVICE_TYPE_TUNEMU:
238                         if(device_type == DEVICE_TYPE_TUNEMU)
239                                 lenin = tunemu_read(device_fd, packet->data + 14, MTU - 14);
240                         else
241 #endif
242                                 lenin = read(device_fd, packet->data + 14, MTU - 14);
243
244                         if(lenin <= 0) {
245                                 logger(LOG_ERR, "Error while reading from %s %s: %s", device_info,
246                                            device, strerror(errno));
247                                 return false;
248                         }
249
250                         switch(packet->data[14] >> 4) {
251                                 case 4:
252                                         packet->data[12] = 0x08;
253                                         packet->data[13] = 0x00;
254                                         break;
255                                 case 6:
256                                         packet->data[12] = 0x86;
257                                         packet->data[13] = 0xDD;
258                                         break;
259                                 default:
260                                         ifdebug(TRAFFIC) logger(LOG_ERR,
261                                                            "Unknown IP version %d while reading packet from %s %s",
262                                                            packet->data[14] >> 4, device_info, device);
263                                         return false;
264                         }
265
266                         memset(packet->data, 0, 12);
267                         packet->len = lenin + 14;
268                         break;
269
270                 case DEVICE_TYPE_TUNIFHEAD: {
271                         u_int32_t type;
272                         struct iovec vector[2] = {{&type, sizeof(type)}, {packet->data + 14, MTU - 14}};
273
274                         if((lenin = readv(device_fd, vector, 2)) <= 0) {
275                                 logger(LOG_ERR, "Error while reading from %s %s: %s", device_info,
276                                            device, strerror(errno));
277                                 return false;
278                         }
279
280                         switch (ntohl(type)) {
281                                 case AF_INET:
282                                         packet->data[12] = 0x08;
283                                         packet->data[13] = 0x00;
284                                         break;
285
286                                 case AF_INET6:
287                                         packet->data[12] = 0x86;
288                                         packet->data[13] = 0xDD;
289                                         break;
290
291                                 default:
292                                         ifdebug(TRAFFIC) logger(LOG_ERR,
293                                                            "Unknown address family %x while reading packet from %s %s",
294                                                            ntohl(type), device_info, device);
295                                         return false;
296                         }
297
298                         memset(packet->data, 0, 12);
299                         packet->len = lenin + 10;
300                         break;
301                 }
302
303                 case DEVICE_TYPE_TAP:
304                         if((lenin = read(device_fd, packet->data, MTU)) <= 0) {
305                                 logger(LOG_ERR, "Error while reading from %s %s: %s", device_info,
306                                            device, strerror(errno));
307                                 return false;
308                         }
309
310                         packet->len = lenin;
311                         break;
312
313                 default:
314                         return false;
315         }
316                 
317         device_total_in += packet->len;
318
319         ifdebug(TRAFFIC) logger(LOG_DEBUG, "Read packet of %d bytes from %s",
320                            packet->len, device_info);
321
322         return true;
323 }
324
325 static bool write_packet(vpn_packet_t *packet) {
326         ifdebug(TRAFFIC) logger(LOG_DEBUG, "Writing packet of %d bytes to %s",
327                            packet->len, device_info);
328
329         switch(device_type) {
330                 case DEVICE_TYPE_TUN:
331                         if(write(device_fd, packet->data + 14, packet->len - 14) < 0) {
332                                 logger(LOG_ERR, "Error while writing to %s %s: %s", device_info,
333                                            device, strerror(errno));
334                                 return false;
335                         }
336                         break;
337
338                 case DEVICE_TYPE_TUNIFHEAD: {
339                         u_int32_t type;
340                         struct iovec vector[2] = {{&type, sizeof(type)}, {packet->data + 14, packet->len - 14}};
341                         int af;
342                         
343                         af = (packet->data[12] << 8) + packet->data[13];
344
345                         switch (af) {
346                                 case 0x0800:
347                                         type = htonl(AF_INET);
348                                         break;
349                                 case 0x86DD:
350                                         type = htonl(AF_INET6);
351                                         break;
352                                 default:
353                                         ifdebug(TRAFFIC) logger(LOG_ERR,
354                                                            "Unknown address family %x while writing packet to %s %s",
355                                                            af, device_info, device);
356                                         return false;
357                         }
358
359                         if(writev(device_fd, vector, 2) < 0) {
360                                 logger(LOG_ERR, "Can't write to %s %s: %s", device_info, device,
361                                            strerror(errno));
362                                 return false;
363                         }
364                         break;
365                 }
366                         
367                 case DEVICE_TYPE_TAP:
368                         if(write(device_fd, packet->data, packet->len) < 0) {
369                                 logger(LOG_ERR, "Error while writing to %s %s: %s", device_info,
370                                            device, strerror(errno));
371                                 return false;
372                         }
373                         break;
374
375 #ifdef ENABLE_TUNEMU
376                 case DEVICE_TYPE_TUNEMU:
377                         if(tunemu_write(device_fd, packet->data + 14, packet->len - 14) < 0) {
378                                 logger(LOG_ERR, "Error while writing to %s %s: %s", device_info,
379                                            device, strerror(errno));
380                                 return false;
381                         }
382                         break;
383 #endif
384
385                 default:
386                         return false;
387         }
388
389         device_total_out += packet->len;
390
391         return true;
392 }
393
394 static void dump_device_stats(void) {
395         logger(LOG_DEBUG, "Statistics for %s %s:", device_info, device);
396         logger(LOG_DEBUG, " total bytes in:  %10"PRIu64, device_total_in);
397         logger(LOG_DEBUG, " total bytes out: %10"PRIu64, device_total_out);
398 }
399
400 const devops_t os_devops = {
401         .setup = setup_device,
402         .close = close_device,
403         .read = read_packet,
404         .write = write_packet,
405         .dump_stats = dump_device_stats,
406 };