Use devname() if available to support devfs cloning on BSD.
[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         logger(LOG_INFO, "%s is a %s", device, device_info);
207
208         return true;
209 }
210
211 static void close_device(void) {
212         switch(device_type) {
213 #ifdef ENABLE_TUNEMU
214                 case DEVICE_TYPE_TUNEMU:
215                         tunemu_close(device_fd);
216                         break;
217 #endif
218                 default:
219                         close(device_fd);
220         }
221
222         free(device);
223         free(iface);
224 }
225
226 static bool read_packet(vpn_packet_t *packet) {
227         int lenin;
228
229         switch(device_type) {
230                 case DEVICE_TYPE_TUN:
231 #ifdef ENABLE_TUNEMU
232                 case DEVICE_TYPE_TUNEMU:
233                         if(device_type == DEVICE_TYPE_TUNEMU)
234                                 lenin = tunemu_read(device_fd, packet->data + 14, MTU - 14);
235                         else
236 #endif
237                                 lenin = read(device_fd, packet->data + 14, MTU - 14);
238
239                         if(lenin <= 0) {
240                                 logger(LOG_ERR, "Error while reading from %s %s: %s", device_info,
241                                            device, strerror(errno));
242                                 return false;
243                         }
244
245                         switch(packet->data[14] >> 4) {
246                                 case 4:
247                                         packet->data[12] = 0x08;
248                                         packet->data[13] = 0x00;
249                                         break;
250                                 case 6:
251                                         packet->data[12] = 0x86;
252                                         packet->data[13] = 0xDD;
253                                         break;
254                                 default:
255                                         ifdebug(TRAFFIC) logger(LOG_ERR,
256                                                            "Unknown IP version %d while reading packet from %s %s",
257                                                            packet->data[14] >> 4, device_info, device);
258                                         return false;
259                         }
260
261                         memset(packet->data, 0, 12);
262                         packet->len = lenin + 14;
263                         break;
264
265                 case DEVICE_TYPE_TUNIFHEAD: {
266                         u_int32_t type;
267                         struct iovec vector[2] = {{&type, sizeof(type)}, {packet->data + 14, MTU - 14}};
268
269                         if((lenin = readv(device_fd, vector, 2)) <= 0) {
270                                 logger(LOG_ERR, "Error while reading from %s %s: %s", device_info,
271                                            device, strerror(errno));
272                                 return false;
273                         }
274
275                         switch (ntohl(type)) {
276                                 case AF_INET:
277                                         packet->data[12] = 0x08;
278                                         packet->data[13] = 0x00;
279                                         break;
280
281                                 case AF_INET6:
282                                         packet->data[12] = 0x86;
283                                         packet->data[13] = 0xDD;
284                                         break;
285
286                                 default:
287                                         ifdebug(TRAFFIC) logger(LOG_ERR,
288                                                            "Unknown address family %x while reading packet from %s %s",
289                                                            ntohl(type), device_info, device);
290                                         return false;
291                         }
292
293                         memset(packet->data, 0, 12);
294                         packet->len = lenin + 10;
295                         break;
296                 }
297
298                 case DEVICE_TYPE_TAP:
299                         if((lenin = read(device_fd, packet->data, MTU)) <= 0) {
300                                 logger(LOG_ERR, "Error while reading from %s %s: %s", device_info,
301                                            device, strerror(errno));
302                                 return false;
303                         }
304
305                         packet->len = lenin;
306                         break;
307
308                 default:
309                         return false;
310         }
311                 
312         device_total_in += packet->len;
313
314         ifdebug(TRAFFIC) logger(LOG_DEBUG, "Read packet of %d bytes from %s",
315                            packet->len, device_info);
316
317         return true;
318 }
319
320 static bool write_packet(vpn_packet_t *packet) {
321         ifdebug(TRAFFIC) logger(LOG_DEBUG, "Writing packet of %d bytes to %s",
322                            packet->len, device_info);
323
324         switch(device_type) {
325                 case DEVICE_TYPE_TUN:
326                         if(write(device_fd, packet->data + 14, packet->len - 14) < 0) {
327                                 logger(LOG_ERR, "Error while writing to %s %s: %s", device_info,
328                                            device, strerror(errno));
329                                 return false;
330                         }
331                         break;
332
333                 case DEVICE_TYPE_TUNIFHEAD: {
334                         u_int32_t type;
335                         struct iovec vector[2] = {{&type, sizeof(type)}, {packet->data + 14, packet->len - 14}};
336                         int af;
337                         
338                         af = (packet->data[12] << 8) + packet->data[13];
339
340                         switch (af) {
341                                 case 0x0800:
342                                         type = htonl(AF_INET);
343                                         break;
344                                 case 0x86DD:
345                                         type = htonl(AF_INET6);
346                                         break;
347                                 default:
348                                         ifdebug(TRAFFIC) logger(LOG_ERR,
349                                                            "Unknown address family %x while writing packet to %s %s",
350                                                            af, device_info, device);
351                                         return false;
352                         }
353
354                         if(writev(device_fd, vector, 2) < 0) {
355                                 logger(LOG_ERR, "Can't write to %s %s: %s", device_info, device,
356                                            strerror(errno));
357                                 return false;
358                         }
359                         break;
360                 }
361                         
362                 case DEVICE_TYPE_TAP:
363                         if(write(device_fd, packet->data, packet->len) < 0) {
364                                 logger(LOG_ERR, "Error while writing to %s %s: %s", device_info,
365                                            device, strerror(errno));
366                                 return false;
367                         }
368                         break;
369
370 #ifdef ENABLE_TUNEMU
371                 case DEVICE_TYPE_TUNEMU:
372                         if(tunemu_write(device_fd, packet->data + 14, packet->len - 14) < 0) {
373                                 logger(LOG_ERR, "Error while writing to %s %s: %s", device_info,
374                                            device, strerror(errno));
375                                 return false;
376                         }
377                         break;
378 #endif
379
380                 default:
381                         return false;
382         }
383
384         device_total_out += packet->len;
385
386         return true;
387 }
388
389 static void dump_device_stats(void) {
390         logger(LOG_DEBUG, "Statistics for %s %s:", device_info, device);
391         logger(LOG_DEBUG, " total bytes in:  %10"PRIu64, device_total_in);
392         logger(LOG_DEBUG, " total bytes out: %10"PRIu64, device_total_out);
393 }
394
395 const devops_t os_devops = {
396         .setup = setup_device,
397         .close = close_device,
398         .read = read_packet,
399         .write = write_packet,
400         .dump_stats = dump_device_stats,
401 };