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