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