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