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