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