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