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