Don't try to send MTU probes to unreachable nodes.
[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
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         free(device);
145         free(iface);
146 }
147
148 bool read_packet(vpn_packet_t *packet) {
149         int lenin;
150
151         cp();
152
153         switch(device_type) {
154                 case DEVICE_TYPE_TUN:
155                         if((lenin = read(device_fd, packet->data + 14, MTU - 14)) <= 0) {
156                                 logger(LOG_ERR, _("Error while reading from %s %s: %s"), device_info,
157                                            device, strerror(errno));
158                                 return false;
159                         }
160
161                         switch(packet->data[14] >> 4) {
162                                 case 4:
163                                         packet->data[12] = 0x08;
164                                         packet->data[13] = 0x00;
165                                         break;
166                                 case 6:
167                                         packet->data[12] = 0x86;
168                                         packet->data[13] = 0xDD;
169                                         break;
170                                 default:
171                                         ifdebug(TRAFFIC) logger(LOG_ERR,
172                                                            _ ("Unknown IP version %d while reading packet from %s %s"),
173                                                            packet->data[14] >> 4, device_info, device);
174                                         return false;
175                         }
176
177                         packet->len = lenin + 14;
178                         break;
179
180                 case DEVICE_TYPE_TUNIFHEAD: {
181                         u_int32_t type;
182                         struct iovec vector[2] = {{&type, sizeof(type)}, {packet->data + 14, MTU - 14}};
183
184                         if((lenin = readv(device_fd, vector, 2)) <= 0) {
185                                 logger(LOG_ERR, _("Error while reading from %s %s: %s"), device_info,
186                                            device, strerror(errno));
187                                 return false;
188                         }
189
190                         switch (ntohl(type)) {
191                                 case AF_INET:
192                                         packet->data[12] = 0x08;
193                                         packet->data[13] = 0x00;
194                                         break;
195
196                                 case AF_INET6:
197                                         packet->data[12] = 0x86;
198                                         packet->data[13] = 0xDD;
199                                         break;
200
201                                 default:
202                                         ifdebug(TRAFFIC) logger(LOG_ERR,
203                                                            _ ("Unknown address family %x while reading packet from %s %s"),
204                                                            ntohl(type), device_info, device);
205                                         return false;
206                         }
207
208                         packet->len = lenin + 10;
209                         break;
210                 }
211
212                 case DEVICE_TYPE_TAP:
213                         if((lenin = read(device_fd, packet->data, MTU)) <= 0) {
214                                 logger(LOG_ERR, _("Error while reading from %s %s: %s"), device_info,
215                                            device, strerror(errno));
216                                 return false;
217                         }
218
219                         packet->len = lenin;
220                         break;
221
222                 default:
223                         return false;
224         }
225                 
226         device_total_in += packet->len;
227
228         ifdebug(TRAFFIC) logger(LOG_DEBUG, _("Read packet of %d bytes from %s"),
229                            packet->len, device_info);
230
231         return true;
232 }
233
234 bool write_packet(vpn_packet_t *packet)
235 {
236         cp();
237
238         ifdebug(TRAFFIC) logger(LOG_DEBUG, _("Writing packet of %d bytes to %s"),
239                            packet->len, device_info);
240
241         switch(device_type) {
242                 case DEVICE_TYPE_TUN:
243                         if(write(device_fd, packet->data + 14, packet->len - 14) < 0) {
244                                 logger(LOG_ERR, _("Error while writing to %s %s: %s"), device_info,
245                                            device, strerror(errno));
246                                 return false;
247                         }
248                         break;
249
250                 case DEVICE_TYPE_TUNIFHEAD: {
251                         u_int32_t type;
252                         struct iovec vector[2] = {{&type, sizeof(type)}, {packet->data + 14, packet->len - 14}};
253                         int af;
254                         
255                         af = (packet->data[12] << 8) + packet->data[13];
256
257                         switch (af) {
258                                 case 0x0800:
259                                         type = htonl(AF_INET);
260                                         break;
261                                 case 0x86DD:
262                                         type = htonl(AF_INET6);
263                                         break;
264                                 default:
265                                         ifdebug(TRAFFIC) logger(LOG_ERR,
266                                                            _("Unknown address family %x while writing packet to %s %s"),
267                                                            af, device_info, device);
268                                         return false;
269                         }
270
271                         if(writev(device_fd, vector, 2) < 0) {
272                                 logger(LOG_ERR, _("Can't write to %s %s: %s"), device_info, device,
273                                            strerror(errno));
274                                 return false;
275                         }
276                         break;
277                 }
278                         
279                 case DEVICE_TYPE_TAP:
280                         if(write(device_fd, packet->data, packet->len) < 0) {
281                                 logger(LOG_ERR, _("Error while writing to %s %s: %s"), device_info,
282                                            device, strerror(errno));
283                                 return false;
284                         }
285                         break;
286
287                 default:
288                         return false;
289         }
290
291         device_total_out += packet->len;
292
293         return true;
294 }
295
296 void dump_device_stats(void)
297 {
298         cp();
299
300         logger(LOG_DEBUG, _("Statistics for %s %s:"), device_info, device);
301         logger(LOG_DEBUG, _(" total bytes in:  %10d"), device_total_in);
302         logger(LOG_DEBUG, _(" total bytes out: %10d"), device_total_out);
303 }