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