4a03bb7bae9f2716ff991c24f5d268d3c9c8f77b
[tinc] / src / uml_device.c
1 /*
2     device.c -- UML network socket
3     Copyright (C) 2002-2005 Ivo Timmermans,
4                   2002-2012 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 along
17     with this program; if not, write to the Free Software Foundation, Inc.,
18     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 */
20
21 #include "system.h"
22
23 #include <sys/un.h>
24
25 #include "conf.h"
26 #include "device.h"
27 #include "net.h"
28 #include "logger.h"
29 #include "utils.h"
30 #include "route.h"
31 #include "xalloc.h"
32
33 static int listen_fd = -1;
34 static int request_fd = -1;
35 static int data_fd = -1;
36 static int write_fd = -1;
37 static int state = 0;
38 static const char *device_info = "UML network socket";
39
40 extern char *identname;
41 extern volatile bool running;
42
43 static uint64_t device_total_in = 0;
44 static uint64_t device_total_out = 0;
45
46 enum request_type { REQ_NEW_CONTROL };
47
48 static struct request {
49         uint32_t magic;
50         uint32_t version;
51         enum request_type type;
52         struct sockaddr_un sock;
53 } request;
54
55 static struct sockaddr_un data_sun;
56
57 static bool setup_device(void) {
58         struct sockaddr_un listen_sun;
59         static const int one = 1;
60         struct {
61                 char zero;
62                 int pid;
63                 int usecs;
64         } name;
65         struct timeval tv;
66
67         if(!get_config_string(lookup_config(config_tree, "Device"), &device)) {
68                 xasprintf(&device, RUNSTATEDIR "/%s.umlsocket", identname);
69         }
70
71         get_config_string(lookup_config(config_tree, "Interface"), &iface);
72
73         if((write_fd = socket(PF_UNIX, SOCK_DGRAM, 0)) < 0) {
74                 logger(LOG_ERR, "Could not open write %s: %s", device_info, strerror(errno));
75                 running = false;
76                 return false;
77         }
78
79 #ifdef FD_CLOEXEC
80         fcntl(write_fd, F_SETFD, FD_CLOEXEC);
81 #endif
82
83         setsockopt(write_fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
84
85         if(fcntl(write_fd, F_SETFL, O_NONBLOCK) < 0) {
86                 logger(LOG_ERR, "System call `%s' failed: %s", "fcntl", strerror(errno));
87                 running = false;
88                 return false;
89         }
90
91         if((data_fd = socket(PF_UNIX, SOCK_DGRAM, 0)) < 0) {
92                 logger(LOG_ERR, "Could not open data %s: %s", device_info, strerror(errno));
93                 running = false;
94                 return false;
95         }
96
97 #ifdef FD_CLOEXEC
98         fcntl(data_fd, F_SETFD, FD_CLOEXEC);
99 #endif
100
101         setsockopt(data_fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
102
103         if(fcntl(data_fd, F_SETFL, O_NONBLOCK) < 0) {
104                 logger(LOG_ERR, "System call `%s' failed: %s", "fcntl", strerror(errno));
105                 running = false;
106                 return false;
107         }
108
109         name.zero = 0;
110         name.pid = getpid();
111         gettimeofday(&tv, NULL);
112         name.usecs = tv.tv_usec;
113         data_sun.sun_family = AF_UNIX;
114         memcpy(&data_sun.sun_path, &name, sizeof(name));
115
116         if(bind(data_fd, (struct sockaddr *)&data_sun, sizeof(data_sun)) < 0) {
117                 logger(LOG_ERR, "Could not bind data %s: %s", device_info, strerror(errno));
118                 running = false;
119                 return false;
120         }
121
122         if((listen_fd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0) {
123                 logger(LOG_ERR, "Could not open %s: %s", device_info,
124                        strerror(errno));
125                 return false;
126         }
127
128 #ifdef FD_CLOEXEC
129         fcntl(device_fd, F_SETFD, FD_CLOEXEC);
130 #endif
131
132         setsockopt(listen_fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
133
134         if(fcntl(listen_fd, F_SETFL, O_NONBLOCK) < 0) {
135                 logger(LOG_ERR, "System call `%s' failed: %s", "fcntl", strerror(errno));
136                 return false;
137         }
138
139         listen_sun.sun_family = AF_UNIX;
140         strncpy(listen_sun.sun_path, device, sizeof(listen_sun.sun_path));
141
142         if(bind(listen_fd, (struct sockaddr *)&listen_sun, sizeof(listen_sun)) < 0) {
143                 logger(LOG_ERR, "Could not bind %s to %s: %s", device_info, device, strerror(errno));
144                 return false;
145         }
146
147         if(listen(listen_fd, 1) < 0) {
148                 logger(LOG_ERR, "Could not listen on %s %s: %s", device_info, device, strerror(errno));
149                 return false;
150         }
151
152         device_fd = listen_fd;
153         state = 0;
154
155         logger(LOG_INFO, "%s is a %s", device, device_info);
156
157         if(routing_mode == RMODE_ROUTER) {
158                 overwrite_mac = true;
159         }
160
161         return true;
162 }
163
164 void close_device(void) {
165         if(listen_fd >= 0) {
166                 close(listen_fd);
167         }
168
169         if(request_fd >= 0) {
170                 close(request_fd);
171         }
172
173         if(data_fd >= 0) {
174                 close(data_fd);
175         }
176
177         if(write_fd >= 0) {
178                 close(write_fd);
179         }
180
181         unlink(device);
182
183         free(device);
184
185         if(iface) {
186                 free(iface);
187         }
188 }
189
190 static bool read_packet(vpn_packet_t *packet) {
191         int lenin;
192
193         switch(state) {
194         case 0: {
195                 struct sockaddr sa;
196                 socklen_t salen = sizeof(sa);
197
198                 request_fd = accept(listen_fd, &sa, &salen);
199
200                 if(request_fd < 0) {
201                         logger(LOG_ERR, "Could not accept connection to %s %s: %s", device_info, device, strerror(errno));
202                         return false;
203                 }
204
205 #ifdef FD_CLOEXEC
206                 fcntl(request_fd, F_SETFD, FD_CLOEXEC);
207 #endif
208
209                 if(fcntl(listen_fd, F_SETFL, O_NONBLOCK) < 0) {
210                         logger(LOG_ERR, "System call `%s' failed: %s", "fcntl", strerror(errno));
211                         running = false;
212                         return false;
213                 }
214
215                 close(listen_fd);
216                 listen_fd = -1;
217                 device_fd = request_fd;
218                 state = 1;
219
220                 return false;
221         }
222
223         case 1: {
224                 if((lenin = read(request_fd, &request, sizeof(request))) != sizeof request) {
225                         logger(LOG_ERR, "Error while reading request from %s %s: %s", device_info,
226                                device, strerror(errno));
227                         running = false;
228                         return false;
229                 }
230
231                 if(request.magic != 0xfeedface || request.version != 3 || request.type != REQ_NEW_CONTROL) {
232                         logger(LOG_ERR, "Unknown magic %x, version %d, request type %d from %s %s",
233                                request.magic, request.version, request.type, device_info, device);
234                         running = false;
235                         return false;
236                 }
237
238                 if(connect(write_fd, &request.sock, sizeof(request.sock)) < 0) {
239                         logger(LOG_ERR, "Could not bind write %s: %s", device_info, strerror(errno));
240                         running = false;
241                         return false;
242                 }
243
244                 write(request_fd, &data_sun, sizeof(data_sun));
245                 device_fd = data_fd;
246
247                 logger(LOG_INFO, "Connection with UML established");
248
249                 state = 2;
250                 return false;
251         }
252
253         case 2: {
254                 if((lenin = read(data_fd, packet->data, MTU)) <= 0) {
255                         logger(LOG_ERR, "Error while reading from %s %s: %s", device_info,
256                                device, strerror(errno));
257                         running = false;
258                         return false;
259                 }
260
261                 packet->len = lenin;
262
263                 device_total_in += packet->len;
264
265                 ifdebug(TRAFFIC) logger(LOG_DEBUG, "Read packet of %d bytes from %s", packet->len,
266                                         device_info);
267
268                 return true;
269         }
270
271         default:
272                 logger(LOG_ERR, "Invalid value for state variable in " FILE);
273                 abort();
274         }
275 }
276
277 static bool write_packet(vpn_packet_t *packet) {
278         if(state != 2) {
279                 ifdebug(TRAFFIC) logger(LOG_DEBUG, "Dropping packet of %d bytes to %s: not connected to UML yet",
280                                         packet->len, device_info);
281                 return false;
282         }
283
284         ifdebug(TRAFFIC) logger(LOG_DEBUG, "Writing packet of %d bytes to %s",
285                                 packet->len, device_info);
286
287         if(write(write_fd, packet->data, packet->len) < 0) {
288                 if(errno != EINTR && errno != EAGAIN) {
289                         logger(LOG_ERR, "Can't write to %s %s: %s", device_info, device, strerror(errno));
290                         running = false;
291                 }
292
293                 return false;
294         }
295
296         device_total_out += packet->len;
297
298         return true;
299 }
300
301 static void dump_device_stats(void) {
302         logger(LOG_DEBUG, "Statistics for %s %s:", device_info, device);
303         logger(LOG_DEBUG, " total bytes in:  %10"PRIu64, device_total_in);
304         logger(LOG_DEBUG, " total bytes out: %10"PRIu64, device_total_out);
305 }
306
307 const devops_t uml_devops = {
308         .setup = setup_device,
309         .close = close_device,
310         .read = read_packet,
311         .write = write_packet,
312         .dump_stats = dump_device_stats,
313 };