01b7071a00687ed1b77b383f1af64ec9d82104a3
[tinc] / src / bsd / device.c
1 /*
2     device.c -- Interaction BSD tun/tap device
3     Copyright (C) 2001-2005 Ivo Timmermans,
4                   2001-2008 Guus Sliepen <guus@tinc-vpn.org>
5
6     This program is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with this program; if not, write to the Free Software
18     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19
20     $Id: device.c 1398 2004-11-01 15:18:53Z guus $
21 */
22
23 #include "system.h"
24
25 #include "conf.h"
26 #include "logger.h"
27 #include "net.h"
28 #include "route.h"
29 #include "utils.h"
30 #include "xalloc.h"
31
32 #define DEFAULT_DEVICE "/dev/tun0"
33
34 typedef enum device_type {
35         DEVICE_TYPE_TUN,
36         DEVICE_TYPE_TUNIFHEAD,
37         DEVICE_TYPE_TAP,
38 } device_type_t;
39
40 int device_fd = -1;
41 char *device = NULL;
42 char *iface = NULL;
43 static char *device_info = NULL;
44 static int device_total_in = 0;
45 static int device_total_out = 0;
46 #if defined(HAVE_OPENBSD) || defined(HAVE_FREEBSD)
47 static device_type_t device_type = DEVICE_TYPE_TUNIFHEAD;
48 #else
49 static device_type_t device_type = DEVICE_TYPE_TUN;
50 #endif
51
52 bool setup_device(void) {
53         char *type;
54
55         cp();
56
57         if(!get_config_string(lookup_config(config_tree, "Device"), &device))
58                 device = xstrdup(DEFAULT_DEVICE);
59
60         if(!get_config_string(lookup_config(config_tree, "Interface"), &iface))
61                 iface = xstrdup(rindex(device, '/') ? rindex(device, '/') + 1 : device);
62
63         if((device_fd = open(device, O_RDWR | O_NONBLOCK)) < 0) {
64                 logger(LOG_ERR, _("Could not open %s: %s"), device, strerror(errno));
65                 return false;
66         }
67
68         if(get_config_string(lookup_config(config_tree, "DeviceType"), &type)) {
69                 if(!strcasecmp(type, "tun"))
70                         /* use default */;      
71                 else if(!strcasecmp(type, "tunnohead"))
72                         device_type = DEVICE_TYPE_TUN;
73                 else if(!strcasecmp(type, "tunifhead"))
74                         device_type = DEVICE_TYPE_TUNIFHEAD;
75                 else if(!strcasecmp(type, "tap"))
76                         device_type = DEVICE_TYPE_TAP;
77                 else {
78                         logger(LOG_ERR, _("Unknown device type %s!"), type);
79                         return false;
80                 }
81         } else {
82                 if(strstr(device, "tap") || routing_mode != RMODE_ROUTER)
83                         device_type = DEVICE_TYPE_TAP;
84         }
85
86         switch(device_type) {
87                 default:
88                         device_type = DEVICE_TYPE_TUN;
89                 case DEVICE_TYPE_TUN:
90 #ifdef TUNSIFHEAD
91                 {       
92                         const int zero = 0;
93                         if(ioctl(device_fd, TUNSIFHEAD, &zero, sizeof zero) == -1) {
94                                 logger(LOG_ERR, _("System call `%s' failed: %s"), "ioctl", strerror(errno));
95                                 return false;
96                         }
97                 }
98 #endif
99 #if defined(TUNSIFMODE) && defined(IFF_BROADCAST) && defined(IFF_MULTICAST)
100                 {
101                         const int mode = IFF_BROADCAST | IFF_MULTICAST;
102                         ioctl(device_fd, TUNSIFMODE, &mode, sizeof mode);
103                 }
104 #endif
105
106                         device_info = _("Generic BSD tun device");
107                         break;
108                 case DEVICE_TYPE_TUNIFHEAD:
109 #ifdef TUNSIFHEAD
110                 {
111                         const int one = 1;
112                         if(ioctl(device_fd, TUNSIFHEAD, &one, sizeof one) == -1) {
113                                 logger(LOG_ERR, _("System call `%s' failed: %s"), "ioctl", strerror(errno));
114                                 return false;
115                         }
116                 }
117 #endif
118 #if defined(TUNSIFMODE) && defined(IFF_BROADCAST) && defined(IFF_MULTICAST)
119                 {
120                                 const int mode = IFF_BROADCAST | IFF_MULTICAST;
121                                 ioctl(device_fd, TUNSIFMODE, &mode, sizeof mode);
122                 }
123 #endif
124
125                         device_info = _("Generic BSD tun device");
126                         break;
127                 case DEVICE_TYPE_TAP:
128                         if(routing_mode == RMODE_ROUTER)
129                                 overwrite_mac = true;
130                         device_info = _("Generic BSD tap device");
131                         break;
132         }
133
134         logger(LOG_INFO, _("%s is a %s"), device, device_info);
135
136         return true;
137 }
138
139 void close_device(void) {
140         cp();
141
142         close(device_fd);
143 }
144
145 bool read_packet(vpn_packet_t *packet) {
146         int lenin;
147
148         cp();
149
150         switch(device_type) {
151                 case DEVICE_TYPE_TUN:
152                         if((lenin = read(device_fd, packet->data + 14, MTU - 14)) <= 0) {
153                                 logger(LOG_ERR, _("Error while reading from %s %s: %s"), device_info,
154                                            device, strerror(errno));
155                                 return false;
156                         }
157
158                         switch(packet->data[14] >> 4) {
159                                 case 4:
160                                         packet->data[12] = 0x08;
161                                         packet->data[13] = 0x00;
162                                         break;
163                                 case 6:
164                                         packet->data[12] = 0x86;
165                                         packet->data[13] = 0xDD;
166                                         break;
167                                 default:
168                                         ifdebug(TRAFFIC) logger(LOG_ERR,
169                                                            _ ("Unknown IP version %d while reading packet from %s %s"),
170                                                            packet->data[14] >> 4, device_info, device);
171                                         return false;
172                         }
173
174                         packet->len = lenin + 14;
175                         break;
176
177                 case DEVICE_TYPE_TUNIFHEAD: {
178                         u_int32_t type;
179                         struct iovec vector[2] = {{&type, sizeof(type)}, {packet->data + 14, MTU - 14}};
180
181                         if((lenin = readv(device_fd, vector, 2)) <= 0) {
182                                 logger(LOG_ERR, _("Error while reading from %s %s: %s"), device_info,
183                                            device, strerror(errno));
184                                 return false;
185                         }
186
187                         switch (ntohl(type)) {
188                                 case AF_INET:
189                                         packet->data[12] = 0x08;
190                                         packet->data[13] = 0x00;
191                                         break;
192
193                                 case AF_INET6:
194                                         packet->data[12] = 0x86;
195                                         packet->data[13] = 0xDD;
196                                         break;
197
198                                 default:
199                                         ifdebug(TRAFFIC) logger(LOG_ERR,
200                                                            _ ("Unknown address family %x while reading packet from %s %s"),
201                                                            ntohl(type), device_info, device);
202                                         return false;
203                         }
204
205                         packet->len = lenin + 10;
206                         break;
207                 }
208
209                 case DEVICE_TYPE_TAP:
210                         if((lenin = read(device_fd, packet->data, MTU)) <= 0) {
211                                 logger(LOG_ERR, _("Error while reading from %s %s: %s"), device_info,
212                                            device, strerror(errno));
213                                 return false;
214                         }
215
216                         packet->len = lenin;
217                         break;
218
219                 default:
220                         return false;
221         }
222                 
223         device_total_in += packet->len;
224
225         ifdebug(TRAFFIC) logger(LOG_DEBUG, _("Read packet of %d bytes from %s"),
226                            packet->len, device_info);
227
228         return true;
229 }
230
231 bool write_packet(vpn_packet_t *packet)
232 {
233         cp();
234
235         ifdebug(TRAFFIC) logger(LOG_DEBUG, _("Writing packet of %d bytes to %s"),
236                            packet->len, device_info);
237
238         switch(device_type) {
239                 case DEVICE_TYPE_TUN:
240                         if(write(device_fd, packet->data + 14, packet->len - 14) < 0) {
241                                 logger(LOG_ERR, _("Error while writing to %s %s: %s"), device_info,
242                                            device, strerror(errno));
243                                 return false;
244                         }
245                         break;
246
247                 case DEVICE_TYPE_TUNIFHEAD: {
248                         u_int32_t type;
249                         struct iovec vector[2] = {{&type, sizeof(type)}, {packet->data + 14, packet->len - 14}};
250                         int af;
251                         
252                         af = (packet->data[12] << 8) + packet->data[13];
253
254                         switch (af) {
255                                 case 0x0800:
256                                         type = htonl(AF_INET);
257                                         break;
258                                 case 0x86DD:
259                                         type = htonl(AF_INET6);
260                                         break;
261                                 default:
262                                         ifdebug(TRAFFIC) logger(LOG_ERR,
263                                                            _("Unknown address family %x while writing packet to %s %s"),
264                                                            af, device_info, device);
265                                         return false;
266                         }
267
268                         if(writev(device_fd, vector, 2) < 0) {
269                                 logger(LOG_ERR, _("Can't write to %s %s: %s"), device_info, device,
270                                            strerror(errno));
271                                 return false;
272                         }
273                         break;
274                 }
275                         
276                 case DEVICE_TYPE_TAP:
277                         if(write(device_fd, packet->data, packet->len) < 0) {
278                                 logger(LOG_ERR, _("Error while writing to %s %s: %s"), device_info,
279                                            device, strerror(errno));
280                                 return false;
281                         }
282                         break;
283
284                 default:
285                         return false;
286         }
287
288         device_total_out += packet->len;
289
290         return true;
291 }
292
293 void dump_device_stats(void)
294 {
295         cp();
296
297         logger(LOG_DEBUG, _("Statistics for %s %s:"), device_info, device);
298         logger(LOG_DEBUG, _(" total bytes in:  %10d"), device_total_in);
299         logger(LOG_DEBUG, _(" total bytes out: %10d"), device_total_out);
300 }