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