5bd253cfe52c8e7ca6a8f7edb8f8dd28fc99f078
[tinc] / src / bsd / device.c
1 /*
2     device.c -- Interaction BSD tun/tap device
3     Copyright (C) 2001-2005 Ivo Timmermans,
4                   2001-2007 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
31 #define DEFAULT_DEVICE "/dev/tun0"
32
33 typedef enum device_type {
34         DEVICE_TYPE_TUN,
35         DEVICE_TYPE_TUNIFHEAD,
36         DEVICE_TYPE_TAP,
37 } device_type_t;
38
39 int device_fd = -1;
40 char *device;
41 char *iface;
42 char *device_info;
43 static int device_total_in = 0;
44 static int device_total_out = 0;
45 #ifdef HAVE_OPENBSD
46 static device_type_t device_type = DEVICE_TYPE_TUNIFHEAD;
47 #else
48 static device_type_t device_type = DEVICE_TYPE_TUN;
49 #endif
50
51 bool setup_device(void) {
52         char *type;
53
54         cp();
55
56         if(!get_config_string(lookup_config(config_tree, "Device"), &device))
57                 device = DEFAULT_DEVICE;
58
59         if(!get_config_string(lookup_config(config_tree, "Interface"), &iface))
60                 iface = rindex(device, '/') ? rindex(device, '/') + 1 : device;
61
62         if((device_fd = open(device, O_RDWR | O_NONBLOCK)) < 0) {
63                 logger(LOG_ERR, _("Could not open %s: %s"), device, strerror(errno));
64                 return false;
65         }
66
67         if(get_config_string(lookup_config(config_tree, "DeviceType"), &type)) {
68                 if(!strcasecmp(type, "tun"))
69                         /* use default */;      
70                 else if(!strcasecmp(type, "tunnohead"))
71                         device_type = DEVICE_TYPE_TUN;
72                 else if(!strcasecmp(type, "tunifhead"))
73                         device_type = DEVICE_TYPE_TUNIFHEAD;
74                 else if(!strcasecmp(type, "tap"))
75                         device_type = DEVICE_TYPE_TAP;
76                 else {
77                         logger(LOG_ERR, _("Unknown device type %s!"), type);
78                         return false;
79                 }
80         } else {
81                 if(strstr(device, "tap"))
82                         device_type = DEVICE_TYPE_TAP;
83         }
84
85         switch(device_type) {
86                 default:
87                         device_type = DEVICE_TYPE_TUN;
88                 case DEVICE_TYPE_TUN:
89 #ifdef TUNSIFHEAD
90                 {       
91                         const int zero = 0;
92                         if(ioctl(device_fd, TUNSIFHEAD, &zero, sizeof zero) == -1) {
93                                 logger(LOG_ERR, _("System call `%s' failed: %s"), "ioctl", strerror(errno));
94                                 return false;
95                         }
96                 }
97 #endif
98 #if defined(TUNSIFMODE) && defined(IFF_BROADCAST) && defined(IFF_MULTICAST)
99                 {
100                         const int mode = IFF_BROADCAST | IFF_MULTICAST;
101                         ioctl(device_fd, TUNSIFMODE, &mode, sizeof mode);
102                 }
103 #endif
104
105                         device_info = _("Generic BSD tun device");
106                         break;
107                 case DEVICE_TYPE_TUNIFHEAD:
108 #ifdef TUNSIFHEAD
109                 {
110                         const int one = 1;
111                         if(ioctl(device_fd, TUNSIFHEAD, &one, sizeof one) == -1) {
112                                 logger(LOG_ERR, _("System call `%s' failed: %s"), "ioctl", strerror(errno));
113                                 return false;
114                         }
115                 }
116 #endif
117 #if defined(TUNSIFMODE) && defined(IFF_BROADCAST) && defined(IFF_MULTICAST)
118                 {
119                                 const int mode = IFF_BROADCAST | IFF_MULTICAST;
120                                 ioctl(device_fd, TUNSIFMODE, &mode, sizeof mode);
121                 }
122 #endif
123
124                         device_info = _("Generic BSD tun device");
125                         break;
126                 case DEVICE_TYPE_TAP:
127                         if(routing_mode == RMODE_ROUTER)
128                                 overwrite_mac = true;
129                         device_info = _("Generic BSD tap device");
130                         break;
131         }
132
133         logger(LOG_INFO, _("%s is a %s"), device, device_info);
134
135         return true;
136 }
137
138 void close_device(void) {
139         cp();
140
141         close(device_fd);
142 }
143
144 bool read_packet(vpn_packet_t *packet) {
145         int lenin;
146
147         cp();
148
149         switch(device_type) {
150                 case DEVICE_TYPE_TUN:
151                         if((lenin = read(device_fd, packet->data + 14, MTU - 14)) <= 0) {
152                                 logger(LOG_ERR, _("Error while reading from %s %s: %s"), device_info,
153                                            device, strerror(errno));
154                                 return false;
155                         }
156
157                         switch(packet->data[14] >> 4) {
158                                 case 4:
159                                         packet->data[12] = 0x08;
160                                         packet->data[13] = 0x00;
161                                         break;
162                                 case 6:
163                                         packet->data[12] = 0x86;
164                                         packet->data[13] = 0xDD;
165                                         break;
166                                 default:
167                                         ifdebug(TRAFFIC) logger(LOG_ERR,
168                                                            _ ("Unknown IP version %d while reading packet from %s %s"),
169                                                            packet->data[14] >> 4, device_info, device);
170                                         return false;
171                         }
172
173                         packet->len = lenin + 14;
174                         break;
175
176                 case DEVICE_TYPE_TUNIFHEAD: {
177                         u_int32_t type;
178                         struct iovec vector[2] = {{&type, sizeof(type)}, {packet->data + 14, MTU - 14}};
179
180                         if((lenin = readv(device_fd, vector, 2)) <= 0) {
181                                 logger(LOG_ERR, _("Error while reading from %s %s: %s"), device_info,
182                                            device, strerror(errno));
183                                 return false;
184                         }
185
186                         switch (ntohl(type)) {
187                                 case AF_INET:
188                                         packet->data[12] = 0x08;
189                                         packet->data[13] = 0x00;
190                                         break;
191
192                                 case AF_INET6:
193                                         packet->data[12] = 0x86;
194                                         packet->data[13] = 0xDD;
195                                         break;
196
197                                 default:
198                                         ifdebug(TRAFFIC) logger(LOG_ERR,
199                                                            _ ("Unknown address family %x while reading packet from %s %s"),
200                                                            ntohl(type), device_info, device);
201                                         return false;
202                         }
203
204                         packet->len = lenin + 10;
205                         break;
206                 }
207
208                 case DEVICE_TYPE_TAP:
209                         if((lenin = read(device_fd, packet->data, MTU)) <= 0) {
210                                 logger(LOG_ERR, _("Error while reading from %s %s: %s"), device_info,
211                                            device, strerror(errno));
212                                 return false;
213                         }
214
215                         packet->len = lenin;
216                         break;
217
218                 default:
219                         return false;
220         }
221                 
222         device_total_in += packet->len;
223
224         ifdebug(TRAFFIC) logger(LOG_DEBUG, _("Read packet of %d bytes from %s"),
225                            packet->len, device_info);
226
227         return true;
228 }
229
230 bool write_packet(vpn_packet_t *packet)
231 {
232         cp();
233
234         ifdebug(TRAFFIC) logger(LOG_DEBUG, _("Writing packet of %d bytes to %s"),
235                            packet->len, device_info);
236
237         switch(device_type) {
238                 case DEVICE_TYPE_TUN:
239                         if(write(device_fd, packet->data + 14, packet->len - 14) < 0) {
240                                 logger(LOG_ERR, _("Error while writing to %s %s: %s"), device_info,
241                                            device, strerror(errno));
242                                 return false;
243                         }
244                         break;
245
246                 case DEVICE_TYPE_TUNIFHEAD: {
247                         u_int32_t type;
248                         struct iovec vector[2] = {{&type, sizeof(type)}, {packet->data + 14, packet->len - 14}};
249                         int af;
250                         
251                         af = (packet->data[12] << 8) + packet->data[13];
252
253                         switch (af) {
254                                 case 0x0800:
255                                         type = htonl(AF_INET);
256                                         break;
257                                 case 0x86DD:
258                                         type = htonl(AF_INET6);
259                                         break;
260                                 default:
261                                         ifdebug(TRAFFIC) logger(LOG_ERR,
262                                                            _("Unknown address family %x while writing packet to %s %s"),
263                                                            af, device_info, device);
264                                         return false;
265                         }
266
267                         if(writev(device_fd, vector, 2) < 0) {
268                                 logger(LOG_ERR, _("Can't write to %s %s: %s"), device_info, device,
269                                            strerror(errno));
270                                 return false;
271                         }
272                         break;
273                 }
274                         
275                 case DEVICE_TYPE_TAP:
276                         if(write(device_fd, packet->data, packet->len) < 0) {
277                                 logger(LOG_ERR, _("Error while writing to %s %s: %s"), device_info,
278                                            device, strerror(errno));
279                                 return false;
280                         }
281                         break;
282
283                 default:
284                         return false;
285         }
286
287         device_total_out += packet->len;
288
289         return true;
290 }
291
292 void dump_device_stats(void)
293 {
294         cp();
295
296         logger(LOG_DEBUG, _("Statistics for %s %s:"), device_info, device);
297         logger(LOG_DEBUG, _(" total bytes in:  %10d"), device_total_in);
298         logger(LOG_DEBUG, _(" total bytes out: %10d"), device_total_out);
299 }