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